gpt4 book ai didi

php - 如何使用 php 从 mySQL 表中提取特定值?

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:40 25 4
gpt4 key购买 nike

所以我已经连接到我的 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格式打印结果?

最佳答案

$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 之间进行选择。

关于php - 如何使用 php 从 mySQL 表中提取特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21018601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com