因此,我已经连接到我的mySQL数据库,并且能够查看表中的所有列,我希望从中提取信息。现在,我需要能够从"ds_users“中的特定字段(即”密码“和”用户名“)读取所有的值。我想把它们存储在一个数组中,然后打印出来。下面是我到目前为止掌握的代码:
$result = mysql_query("SHOW COLUMNS FROM ds_users");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
}另外,有没有一种方法可以以JSON格式打印结果?
发布于 2014-01-09 11:22:21
$result = mysql_query("SELECT username, password FROM ds_users");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$dataArray['user'] = $row->user;
$dataArray['password'] = $row->password;
}
print_r(json_encode($dataArray));
}附带注意: mysql函数是不推荐的,您应该在mysqli或PDO之间进行选择。
发布于 2014-01-09 11:16:48
在php中使用编码,编码()
$arr = array();
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$arr[] = $row;
}
}
print_r(json_encode($arr));发布于 2014-01-09 11:16:50
存储在数组中
$dataArray = array();
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$dataArray[] = $row;
}
}使用编码,编码函数转换为JSON格式
$jsonString = json_encode($dataArray);https://stackoverflow.com/questions/21018601
复制相似问题