0
|touser| fromuser |  msg  |
|  A   |    B     | hi    |
|  A   |    B     | hello |
|  C   |    D     | bye   |
|  C   |    E     | hey   |

when i use following query in mysql workbench it shows the desired result that is all the rows with given name:

select * from db.table1 where touser in ('A');

output:

|touser| fromuser |  msg  |
|  A   |    B     | hi    |
|  A   |    B     | hello |

but when i pass query from php commands the resultant array contains only first record

<?php
 //connection establishing commands
 $sql="select * from db.table1 where touser in ('A')";

 $result=mysqli_query($link, $sql);

 $data=mysqli_fetch_array($result,MYSQLI_NUM);

 print_r($data);

 //other stuff;
 ?>

output:

    Array ( [0] => A [1] => B [2] => Hi )

am I missing something in the php commands?

2
  • dont use mysqli functions now...it is not recommanded nowadays... Commented Sep 19, 2013 at 19:27
  • @shwetdalal are you saying to use PDO? Commented Sep 19, 2013 at 19:36

1 Answer 1

2

You're PHP is simply returning the first row of the MySQL result set.

You'll want to replace $data=mysqli_fetch_array($result,MYSQLI_NUM);

with

while ($data = mysqli_fetch_array($result, MYSQLI_NUM)) {
    print_r($data);
}

Which will iterate over each row of the result set. In other words, the mysqli_fetch_array function doesn't fetch the entire result set as an array, it simply returns a single row, and then moves the row "pointer" to the next row.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.