[英]how to fetch multiple rows with same field value from a mysql database using php
|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: 当我在mysql workbench中使用以下查询时,它显示了所有具有给定名称的行的结果:
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命令传递查询时,结果数组只包含第一条记录
<?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? 我在php命令中遗漏了什么?
You're PHP is simply returning the first row of the MySQL result set. 你是PHP只是返回MySQL结果集的第一行。
You'll want to replace $data=mysqli_fetch_array($result,MYSQLI_NUM);
你想要替换
$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.
换句话说,mysqli_fetch_array函数不会将整个结果集作为数组获取,它只返回一行,然后将行“指针”移动到下一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.