gpt4 book ai didi

php - 概念 - 组织数据库

转载 作者:行者123 更新时间:2023-11-29 13:35:56 27 4
gpt4 key购买 nike

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





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

(31 个回答)


8年前关闭。




我对 php 很陌生,我有这段代码。我不知道数据是否是从数据库中获取的。

$sql = mysql_query("SELECT id,question,date,user,numberofcomments FROM questions");
if (!$sql) {
echo 'Could not run query: ' . mysql_error();
exit;
}

$result = mysql_fetch_row($sql);

echo '<table border="1">
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>';

while($row = mysql_fetch_assoc($result))
{
echo '<tr>';
echo '<td class="leftpart">';
echo '<h3><a href="topic.php?id=' . $row[id] . '">' . $row['question'] . '</a><h3>';
echo '</td>';
echo '<td class="rightpart">';
echo $row['date'];
echo '</td>';
echo '</tr>';
}

我收到这个错误。

警告:mysql_fetch_assoc() 期望参数 1 是资源,数组在

有人可以解释这个错误。我将 while($row...帮助文件让我感到困惑,所以我来到这里。

我做错了什么,我可以改变什么?

编辑:

我已连接到数据库(我已经提取了用户名和密码的信息)

最佳答案

mysql_fetch_assoc() 期望在 mysql_query() 返回的资源中传递.因此,您的电话应该是

while($row = mysql_fetch_assoc($sql))

相反,您传入的是 mysql_fetch_row() 的返回值这是一个数组或 bool 值。

PS - 全部 mysql_*功能已弃用/正在弃用。你不应该使用它们。

关于php - 概念 - 组织数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18753444/

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