MYSTERIOUS RESULT FROM SQL

Hi all, I’m new to PHP have written some code to extract data from mysql. here it is

<?php echo "Testing Database....
"; $con = mysqli_connect('localhost', 'db_id', 'my_pw', 'mysql_db'); $result = $con->query("SELECT user_id FROM users"); echo "Number of users : ".$result->num_rows."

"; $row = mysqli_fetch_row($result);
        echo $row[0]."<br>";
        echo $row[1]."<br>";
        echo $row[2]."<br>";

    ?>
</body>

in result number of rows its giving right, but when i fetch the result in row, its giving only first row. rest all are empty.

then further i have tested few more things like, i change the query as below

‘SELECT * FROM users’

now this is giving another strange result. It selected first row of the table and making array for all available columns.

i have attached herewith image of my table, and result of both of my queries.

Could someone guide me on this issue…


result 1.png

result 2.png

Table.png

You need to use a loop and array instead of row.

Change this:
[php]$row = mysqli_fetch_row($result);[/php]
to this:
[php]while($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
// using MYSQLI_BOTH allows for alpha or numeric arrays
echo $row[0]."
";
echo $row[‘user_id’]."
";
}[/php]

Hope that helps,
Red :wink:

Thanks Redscouse, yeah its solved the problem…

You’re welcome, happy to help :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service