Jump to content

FETCH ARRAY RESULT


owaisakber

Recommended Posts

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

<!DOCTYPE html>
<html lang="en">post-173352-0-21519900-1414409660_thumb.pngpost-173352-0-51246100-1414409661_thumb.pngpost-173352-0-10550800-1414410158_thumb.png
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <?php
            echo "Testing Database....<br>";
            $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."<br><br>";
            $row = mysqli_fetch_row($result);

            echo $row[0]."<br>";
            echo $row[1]."<br>";
            echo $row[2]."<br>";

        ?>
    </body>
</html>

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...

Link to comment
https://forums.phpfreaks.com/topic/292091-fetch-array-result/
Share on other sites

The result is an array so without a loop you will only get 1 result

 

replace

$row = mysqli_fetch_row($result);

            echo $row[0]."<br>";
            echo $row[1]."<br>";
            echo $row[2]."<br>"; 

with

/* fetch associative array */
    while ($row = mysqli_fetch_row($result)) {
        echo $row[0]."<br />";
    }
Link to comment
https://forums.phpfreaks.com/topic/292091-fetch-array-result/#findComment-1494907
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.