-2

Good day good people.I have unkown error while trying to display data from mysql to web using PHP.Error is:

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\form_2\guestbook.php on line 17. Line 17 is like this:

while($row=mysqli_fetch_array($result))

I really don't know where the problem is, so i hope u will help me.Thank u all :D

Here is code i'am using Here is code that i am using.I just want to display data from database to web.

<?php
$con=mysqli_connect("localhost","root","","php_tests");
if (mysqli_connect_errno())
{
echo"Error connecting to database". mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM test_mysql");
while($row=mysqli_fetch_array($result))
{
$ime=$row['name'];
$prezime=$row['lastname'];
$id_number=$row['id'];
echo'<table border="1" width="33%">
<tr>
    <td>Ime:</td>
    <td>'.$ime.'</td>
</tr>
<tr>
    <td>Prezime</td>
    <td>'.$prezime.'</td>
</tr>

</table>
';
}
mysqli_close($con);

?>
Dharman
  • 26,923
  • 21
  • 73
  • 125
RIfet
  • 9
  • 4

1 Answers1

0

It means your query execution failed. You need to check if condition the result was false before trying to use it.

PHP

if (!mysqli_fetch_array($result)) {
    printf("Error: %s\n", mysqli_fetch_array($con));
    exit();
} else{
    while($row=mysqli_fetch_array($result)){   
          .....
          // Your code
    }  
}

mysqli_fetch_array() returns FALSE if there was an error in the query. So you should test for it before use while loop...


Updated

<?php
    $con=mysqli_connect("localhost","root","","php_tests");
    if (mysqli_connect_errno()){
        echo"Error connecting to database". mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM test_mysql");
    while($row=mysqli_fetch_array($result))
    {
        $ime=$row['name'];
        $prezime=$row['lastname'];
        $id_number=$row['id'];
        echo '<table border="1" width="33%">
        <tr>
            <td>Ime:</td>
            <td>'.$ime.'</td>
        </tr>
        <tr>
            <td>Prezime</td>
            <td>'.$prezime.'</td>
        </tr>

        </table>';
    }
    mysqli_close($con);

?>

Result

enter image description here

PHPMyAdmin

enter image description here

without checking it work perfect but in your case not working so you can try to check if condition. Hope this help you!

Jaykumar Patel
  • 25,525
  • 12
  • 68
  • 75
  • THis worked perfectly, just as i wanted :D – RIfet Mar 30 '14 at 16:19
  • I would like to ask u another thing.Do u know can i make that "id" goes from last one to first (from 6 - 1) i need it for my page so the new stuffs goes on top not on the end.I hope u understeand what i'am trying to say :D – RIfet Mar 30 '14 at 16:25
  • not understand clearly. – Jaykumar Patel Mar 30 '14 at 16:30
  • I will explain it again.I have database in mysql and there i have table with 3 fields.Id, name and lastname.Id is primarykey.And now when i want to list all rows from mysql to website it works, but i want something else.I want to place last insert on the top.Example:If i have 2 rows in database and i want to export it to website it will do it this way: 1. name1 lastname 2. name2 lastname2 and that is not what i want.I want do it so first come 2. name2 lastname2 and then 1. name1 lastname1.I hope it is good now – RIfet Mar 30 '14 at 16:35
  • Try this query "SELECT * FROM test_mysql ORDER BY id DESC" – Jaykumar Patel Mar 30 '14 at 16:38