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
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 />";
    }
Edited by tryingtolearn
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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