Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"mysqli_fetch_array", not working in my code 1

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
IL
Hi everyone,
The following code is the most simple I could get to make me understand how to work with 'mysqli_fetch_array'.
Code:
    <?php
    require ('mysqli_connect.php'); 
    $sql="SELECT 'fname', 'email', 'pass' FROM 'list'";
    $row=mysqli_fetch_array($result,MYSQLI_NUM);
    while ($row !== NULL)
    {
    echo $row[0] . '. ' . $row[1] . ' ' . $row[2];
    echo '<br />';
    }
    mysqli_close($dbcon);
    ?>
That code produces the following error message:
Can anyone advise me how to make that code work? What "MYSQLI_NUM" stands for?
Thanks
 
Oh and why isn't it working?


Dunno, does $result has a value (other than false, which is what the error suggests)?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks,
That code finally worked and why it hadn't worked before I'll compare both codes and have my conclusions.
 
It's probably because your original code, as posted here, missed out the step of converting a query
PHP:
$sql="SELECT 'fname', 'email', 'pass' FROM 'list'";

to a query result object

PHP:
$result=mysqli_query($con,$sql)

So $result was undefined (false) when you tried to use it.

PHP:
$row=mysqli_fetch_array($result,MYSQLI_NUM);



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks ,
Another thing I was not aware of is: you need to run
Code:
$row=mysqli_fetch_array($result,MYSQLI_NUM);
each time in order to step to a next data row...
Thanks again
 
Yep, that's why it is most often run in a "while" loop in 'tutorials'


PHP:
while ($row = .... ) {
....

}

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top