循环通过php表不工作[警告:mysql_fetch_assoc()期望参数1是资源,布尔给定] [英] looping through php table is not working [Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given]
问题描述
我在我的index.php页面上收到此错误,其中我还包括代码
I am getting this error on my index.php page where i have also included the code
<?pho
require 'connect.php';
?>
警告:mysql_fetch_assoc()期望参数1为资源,布尔值
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given
<?php
$sql = "SELECT * FROM 'menulinks'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo '<li>', $row['linktitle'], '<li>';
}
?>
这是我的连接函数
<?php
$con = mysql_connect('localhost', 'root', '') or die('Sorry, we could not connect');
mysql_select_db('philipsnewsite', $con) or die('Sorry, we could not connect');
?>
推荐答案
您已在单引号字元,MySQL将其解释为字符串文字。您应该使用反引号,或者根本不使用引号字符:
You have quoted your table name in single-quote characters, which MySQL interprets as a string literal. You should either use backticks, or no quote characters at all:
$sql = "SELECT * FROM `menulinks`";
此语法错误导致 mysql_query()
函数返回false,你可以测试如下:
This syntax error caused the mysql_query()
function to return false, which you can test as follows:
$result = mysql_query($sql) or die(mysql_error());
另请注意,如大红色框 mysql_query()
:
Also note, as documented in a big red box at the top of the manual page for mysql_query()
:
建议替代方案
使用此扩充功能灰心。而是 MySQLi 或 PDO_MySQL 扩展名。另请参见 MySQL:选择API 指南,并且相关常见问题了解更多信息。此功能的替代方案包括:
Suggested alternatives
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_query()
PDO::query()
这篇关于循环通过php表不工作[警告:mysql_fetch_assoc()期望参数1是资源,布尔给定]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!