获取所有mysql选定的行到一个数组中 [英] Get all mysql selected rows into an array

查看:18
本文介绍了获取所有mysql选定的行到一个数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 php 中是否有一个函数可以让我将所有选定的数据放入一个数组中.目前我正在使用 mysql_fetch_array 并且正如我在手册中所阅读的那样,该函数不会获取表中的每条记录.

I am wondering if there a function in php that can allow me put all my selected data in an array .Currently i am using mysql_fetch_array and as i have read in the manual,that function won't fetch every record in the table.

$result = mysql_query("SELECT * FROM $tableName");            
$array = mysql_fetch_array($result);

  echo json_encode($array);

推荐答案

出于性能和安全目的,我建议使用 MySQLi 或 MySQL PDO,但要回答这个问题:

I would suggest the use of MySQLi or MySQL PDO for performance and security purposes, but to answer the question:

while($row = mysql_fetch_assoc($result)){
     $json[] = $row;
}

echo json_encode($json);

如果你切换到 MySQLi,你可以这样做:

If you switched to MySQLi you could do:

$query = "SELECT * FROM table";
$result = mysqli_query($db, $query);

$json = mysqli_fetch_all ($result, MYSQLI_ASSOC);
echo json_encode($json );

这篇关于获取所有mysql选定的行到一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆