循环通过php表不工作[警告:mysql_fetch_assoc()期望参数1是资源,给出布尔值][英] looping through php table is not working [Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given]

本文是小编为大家收集整理的关于循环通过php表不工作[警告:mysql_fetch_assoc()期望参数1是资源,给出布尔值]的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在我的index.php页面上遇到此错误,我还包括代码

<?pho
require 'connect.php';
?>

警告:mysql_fetch_assoc()期望参数1为资源,布尔值给定

<?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将其解释为字符串字面.您应该使用backticks,或者根本不使用引用字符:

$sql = "SELECT * FROM `menulinks`";

此语法错误导致mysql_query()函数返回false,您可以按以下方式测试:

$result = mysql_query($sql) or die(mysql_error());

另外,请注意,如A Big Red Box 在手动页面顶部的 mysql_query() :

建议的替代品

不建议使用此扩展.相反, mysqli or or mysql:选择API 指南和相关的常见问题以获取更多信息.此功能的替代方案包括:

本文地址:https://www.itbaoku.cn/post/597318.html

问题描述

I am getting this error on my index.php page where i have also included the code

<?pho
require 'connect.php';
?>

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>';

}
    ?>

Here is my connect function

<?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');

?>

推荐答案

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`";

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());

Also note, as documented in a big red box at the top of the manual page for mysql_query():

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:

相关标签/搜索