0

i can retrieve data from oracle db by php when i try do loop to get data it give me error

undefined index: id in C:\xampp\htdocs\testing\test.php on line 15

here is my code

<?php
$username = "kemo";
$password = "kemoacer77";
$server = "localhost/XE";
$conn = oci_connect($username, $password, $server);
if(!$conn){
die("connect error".oci_error());
}

$stid = oci_parse($conn, 'SELECT id, username FROM users');
oci_execute($stid);

while (($row = oci_fetch_array($stid, OCI_BOTH))) {
    // Use the uppercase column names for the associative array indices
    echo  $row['id'] ;
    echo $row['username'];
}

oci_free_statement($stid);
oci_close($conn);


?>

2 Answers 2

1

The documentation of oci_fetch_array() says :

Oracle's default, non-case sensitive column names will have uppercase associative indices in the result array. Case-sensitive column names will have array indices using the exact column case.
Use var_dump() on the result array to verify the appropriate case to use for each query.

And the comment in your code also says :

// Use the uppercase column names for the associative array indices


So, why are you using lowercase column names ?

This is your code :

echo  $row['id'] ;
echo $row['username'];

According to the comment in your code, and the note in the manual, should you not use uppercase, like this :

echo  $row['ID'] ;
echo $row['USERNAME'];


And, if this still doesn't work, just do as said in the manual : use var_dump() in your loop, to see how your data looks like :

while (($row = oci_fetch_array($stid, OCI_BOTH))) {
    var_dump($row);
}
Sign up to request clarification or add additional context in comments.

4 Comments

wow all right now when change username, id to capital character USERNAME, ID its run good
but how i can check if query have error in php + mysql i use this way if(!$query){die(mysql_error())} how can do it with oracle
It seems oci_execute() returns false on failure : php.net/oci_execute
Well, if there is an error in the SQL query, it fails -- and that function will return false, I suppose (just use some invalid SQL Query, and you'll know :-) )
0

In my case I used NVL

The Oracle/PLSQL NVL function lets you substitute a value when a null value is encountered

NVL( string1, replace_with )

SELECT NVL(commission, 0) FROM sales;

Output:

123 223 323

423

answer would be like 123 223 323 0 423

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.