gpt4 book ai didi

PHP - mysql_fetch_assoc() 错误

转载 作者:行者123 更新时间:2023-11-29 04:19:59 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource

(31 个回答)


7年前关闭。




我尝试这个查看单行数据:

include("../engine/setconnect.php");

$sql = "SELECT id_member,username FROM account WHERE id_member=".$_GET['id_member'];

$result = mysql_query($sql);
$data = mysql_fetch_assoc($result);

echo "$data['id_member']";
echo "$data['name']";

但结果总是:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in

“id_member”中的数据是字母数字

如何解决这个问题? :(

最佳答案

如果该字段确实是字母数字,那么您应该包括引号:

id_member = '".$_GET['id_member'] . "'";

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Ref: https://stackoverflow.com/a/12860140/3859027



这是一个使用 PDO 和准备好的语句的简单示例:
if(isset($_GET['id_member'])) {

$db = new PDO('mysql:host=localhost;dbname=DATABASE_NAME', 'username', 'password');

$sql = "SELECT id_member,username FROM account WHERE id_member = :id_member";

$select = $db->prepare($sql);
$select->bindParam(':id_member', $_GET['id_member']);
$select->execute();

$result = $select->fetch(PDO::FETCH_ASSOC);
echo $result['username'];
}

关于PHP - mysql_fetch_assoc() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27199966/

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