Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql_query()返回true,但mysql_num_rows()和mysql_fetch_array()给出";不是有效的资源错误_Php_Mysql - Fatal编程技术网

Php mysql_query()返回true,但mysql_num_rows()和mysql_fetch_array()给出";不是有效的资源错误

Php mysql_query()返回true,但mysql_num_rows()和mysql_fetch_array()给出";不是有效的资源错误,php,mysql,Php,Mysql,以下是相关代码: 从index.php: require_once('includes/DbConnector.php'); // Create an object (instance) of the DbConnector $connector = new DbConnector(); // Execute the query to retrieve articles $query1 = "SELECT id, title FROM articles ORDER BY id DESC LI

以下是相关代码:

从index.php:

require_once('includes/DbConnector.php');

// Create an object (instance) of the DbConnector
$connector = new DbConnector();

// Execute the query to retrieve articles
$query1 = "SELECT id, title FROM articles ORDER BY id DESC LIMIT 0,5";
$result = $connector->query($query1);

echo "vardump1:";
var_dump($result);
echo "\n";

/*(!line 17!)*/ echo "Number of rows in the result of the query:".mysql_num_rows($result)."\n";
// Get an array containing the results.
// Loop for each item in that array


while ($row = $connector->fetchArray($result)){

echo '<p> <a href="viewArticle.php?id='.$row['id'].'">';
echo $row['title'];
echo '</a> </p>';

从手册中可以了解到,mysql\u查询返回值类型不是布尔值,而是资源。

您的代码中的某些内容将其转换为

与上述内容一致,您的mysql\u查询存在问题。它永远不会变成真的。错误或资源

重构:mysql\u query($query,$this->link)或die(mysql\u error())


在查询后回显mysqlerror(),并查看错误消息。您可能没有有效的连接

您的问题在休养线上:

return mysql_query($query, $this->link) or die(mysql_error())
应该这样写:

$result = mysql_query($query, $this->link);
if(!$result) die(mysql_error());
return $result;

在动态语言中,
首先返回计算结果为true的
对象,这是很常见的,但在PHP中,
X或Y
的结果总是truefalse

你能发布你的查询字符串吗。我打赌链接可能重复,他的db\u select失败了?mysql\u db\u select()在var_dump中给出bool(true),所以不是这样。@Col.Shrapnel-什么可以将查询结果转换为布尔值?我发布了所有$result变量生命周期的代码。顺便说一句,查询语句在phpmyadmin中工作并返回有效结果。@zlance4012没有人质疑您的查询语句。我说的是mysql\u查询函数resultI刚刚意识到,当我回显mysql\u错误()时。。。谢谢大家@zlance4012将其从愚蠢幼稚的die()重构为正确的触发器\u error(),例如:$dbconn=mysql\u pconnect($myhost、$myuser、$mypass);if(!$dbconn)触发\u错误('数据库连接失败',E\u用户\u错误);当您使用trigger_error时,将根据PHP的错误处理例程处理错误。
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /*path to*/DbConnector.php on line 50
return mysql_query($query, $this->link) or die(mysql_error())
$result = mysql_query($query, $this->link);
if(!$result) die(mysql_error());
return $result;