警告:mysql_fetch_array() 期望参数 1 是资源,布尔值在第 18 行 C:\xampp\htdocs\wr\header.php 中给出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10839470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:11:33  来源:igfitidea点击:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\wr\header.php on line 18

php

提问by Juliver Galleto

Possible Duplicate:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

可能重复:
警告:mysql_fetch_array() 期望参数 1 是资源,布尔值在

Ok I have this code.

好的,我有这个代码。

<?//index.php
if(!defined('INCLUDE_CHECK')) die('You are not allowed to execute this file directly');


include('db.php');//include our database
//connecting to the database
$con = mysql_connect($dbhost, $dbusername, $dbpassword);
//if we cant connect
if (!$con)
{
die ('could not connect to the database' . mysql_error());
}
//if successfully connected then select db
mysql_select_db ($dbtable, $con);
//our query
$result = mysql_query("SELECT * FROM header");
//fetch our result
while($row = mysql_fetch_array($result)) //this is the line 18
{
//if type is meta then..
if ($row['type'] === "meta")
{
$meta .= "<meta name='".$row['name']."' content='".$row['content']."' />";
}
//if type is title then..
elseif ($row['type'] === "title")
{
$title = "<title>".$row['content']."</title>";
$title2 = "<div id='ti'>".$row['name']."</div>";
}
//if type is favicon then..
elseif ($row['type'] === "favicon")
{
$favicon = "<link rel='shortcut icon' href='".$row['content']."' />";
$imglogo = "<img class='imglogo' src='".$row['content']."' />";
}
//if type is description then..
elseif ($row['type'] === "description")
{
$des = "<div id='ti2'>".$row['content']."</div>";
}

}
mysql_close($con);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<? echo $title; //line 50 ?>
<? echo $favicon; //line 51 ?>
<? echo $meta; //line 52 ?>
<? echo (file_get_contents("cc.txt")); ?>
</head>

and I got this following errors, Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\wr\header.php on line 18

我得到了以下错误,警告:mysql_fetch_array() 期望参数 1 为资源,布尔值在 C:\xampp\htdocs\wr\header.php 中给出,第 18 行

Notice: Undefined variable: title in C:\xampp\htdocs\wr\header.php on line 50

注意:未定义变量:title in C:\xampp\htdocs\wr\header.php 第 50 行

Notice: Undefined variable: favicon in C:\xampp\htdocs\wr\header.php on line 51

注意:未定义变量:第 51 行 C:\xampp\htdocs\wr\header.php 中的 favicon

Notice: Undefined variable: meta in C:\xampp\htdocs\wr\header.php on line 52

注意:未定义变量:meta in C:\xampp\htdocs\wr\header.php 第 52 行

the error lines are specified along the code, please see the top code for error lines.

错误行在代码中指定,请参阅错误行的顶部代码。

Can some help me into this? thanks in advance.

有人可以帮我解决这个问题吗?提前致谢。

Juliver.

朱利文。

回答by GergelyPolonkai

Maybe your SELECT didn't succeed. You should check if $resultis FALSE

也许您的 SELECT 没有成功。您应该检查是否$result为 FALSE

if ($result === false)

and if so, you can get the error message with mysql_error().

如果是这样,您可以使用mysql_error().

回答by Bob Bobbio

Before passing the $result into mysql_fetch_array, you should always check that it !== false, because this is the value of it if the query fails, which is what it looks is what happened in your case. See http://php.net/manual/en/function.mysql-query.php. If it did fail you might want to echo mysql_error() to see what went wrong.

在将 $result 传递到 mysql_fetch_array 之前,您应该始终检查它是否为 !== false,因为如果查询失败,这是它的值,这就是它看起来的情况,这就是您的情况。请参阅http://php.net/manual/en/function.mysql-query.php。如果它确实失败了,您可能想要回显 mysql_error() 以查看出了什么问题。

回答by scaraveos

From http://php.net/manual/en/function.mysql-query.php:

http://php.net/manual/en/function.mysql-query.php

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

mysql_query() will also fail and return FALSE if the user does not have permission to access > the table(s) referenced by the query.

对于 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回结果集的语句,mysql_query() 在成功时返回资源,在错误时返回 FALSE。

对于其他类型的 SQL 语句,INSERT、UPDATE、DELETE、DROP 等,mysql_query() 在成功时返回 TRUE,在错误时返回 FALSE。

如果用户无权访问 > 查询引用的表,mysql_query() 也将失败并返回 FALSE。

Before further processing the result you should always make sure that it is not boolean FALSE (ie. !== FALSE)

在进一步处理结果之前,您应该始终确保它不是布尔 FALSE(即!== FALSE)

回答by Austin

The error persists in one of the below lines so add:

错误仍然存​​在于以下行之一中,因此添加:

mysql_select_db ($dbtable, $con) OR DIE(mysql_error());

mysql_select_db ($dbtable, $con) OR DIE(mysql_error());

$result = mysql_query("SELECT * FROM header") OR DIE(mysql_error());

$result = mysql_query("SELECT * FROM header") OR DIE(mysql_error());