简体   繁体   English

如何使用php从mysql数据库中获取具有相同字段值的多行

[英]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.

相关问题 Mysql 不获取具有相同字段值的多行 - Mysql do not fetch multiple rows with same field value 如何使用MySQL和PHP以随机顺序从数据库中获取值 - How to fetch value from Database in Random order using MySQL and PHP 如何从MySQL中获取多行,并使它们成为复选框的值,以将其插入php中数据库的另一个表中? - How Do I fetch multiple rows from MySQL and and make them the value of checkbox(es) to be inserted to a another table in the database, in php? 如何使用PHP从带有复选框的mysql数据库中删除多行? - How to delete multiple rows from mysql database with checkbox using PHP? 如何使用相同的字段名更新多个mysql数据库行? - How to update multiple mysql database rows with same field names? 如何使用 php 和 mySQL 从数据库中获取 int 值并在表中显示字符串值而不是该 int 值 - how to fetch int value from database and display string value instead of that int value in table using php and mySQL 如何使用mysql从数据库中获取值,并且该值以相同的形式使用 - how to access fetch value from database using mysql and this value use in same form any were PHP从MySQL获取多行 - php fetch multiple rows from mysql PHP从多个MySQL表中获取行 - Php fetch rows from multiple MySQL tables 如何在一次提交中从PHP表单将相同(多个)行的静态和动态数据插入MySQL数据库 - How to insert static and dynamic data as same (multiple)rows into MySQL database from a PHP form in one submission
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM