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);


?>
user700792
  • 2,199
  • 5
  • 20
  • 15

2 Answers2

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);
}
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • wow all right now when change username, id to capital character USERNAME, ID its run good – user700792 Apr 15 '11 at 20:56
  • 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 – user700792 Apr 15 '11 at 21:01
  • It seems `oci_execute()` returns false on failure : http://www.php.net/oci_execute – Pascal MARTIN Apr 15 '11 at 21:02
  • 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 :-) )* – Pascal MARTIN Apr 16 '11 at 14:15
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