mysql_fetch_row 不返回完整数组
我正在尝试使用以下代码循环遍历 MySQL 中的行。不幸的是, mysql_fetch_row 函数仅返回查询的第一行(我用 print_r 确认了这一点)。当我使用 mysql_num_rows 时,它会返回该查询的正确行数(2)。知道为什么 mysql_fetch_row 只返回第一行吗?
$query = 'SELECT firstName FROM profiles WHERE city="Phoenix" and lastName ="Smith"';
$result = mysql_query($query);
$row = mysql_fetch_row($result);
print_r($row); //This returns only 1 array item: Array ( [0] => John)
$a=mysql_num_rows($result);
print_r($a); //This returns 2
我还在 MySQL 中运行了相同的查询,它返回两行(“John”和“Jim”)。
感谢您的帮助。
I am attempting to loop through rows in MySQL with the following code. Unfortunately, the mysql_fetch_row
function is only returning the first row of my query (I confirmed this with print_r). When I use mysql_num_rows
it returns the correct number of rows for that query(2). Any idea why mysql_fetch_row
is returning only the first row?
$query = 'SELECT firstName FROM profiles WHERE city="Phoenix" and lastName ="Smith"';
$result = mysql_query($query);
$row = mysql_fetch_row($result);
print_r($row); //This returns only 1 array item: Array ( [0] => John)
$a=mysql_num_rows($result);
print_r($a); //This returns 2
I have also run the same query in MySQL and it returns both rows ("John" and "Jim").
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需执行 mysql_fetch_row 两次(或循环执行)。根据定义(如果您阅读文档)它一次仅返回一行。所以:
Just perform
mysql_fetch_row
twice (or in a loop). By definition (if you read the documentation) it returns only one row at once. So:您想将其放入
while
循环:它的作用是对于它找到的每个结果(行),它获取您想要的数据并显示它。因此它获取第一个结果,显示名称,循环回到顶部,获取第二个结果,显示该名称等等,然后一旦没有更多与查询条件匹配的结果,while 循环就会结束。
You want to put it in a
while
loop:What this does is for each result (row) it finds, it fetches the data you want and displays it. So it gets the first result, displays the name, loops back to the top, fetches the second result, displays that name and so on, then once you have no more results that match the query criteria, the while loop is ended.
PHP 按照 SQL 结果的缓冲顺序来访问它们。您需要运行一个循环才能访问所有内容。
如果您希望将所有内容加载到数组中:
PHP accesses the SQL Results in order of how they're buffered. You'll need to run a loop to access all the contents.
If you wish to load everything into an array:
您可以使用以下任意一项:
或
或
You can use any of the following one:
or
or