警告:mysql_fetch_array():提供的参数不是有效的MySQL结果

时间:2021-11-01 16:43:05

I get the error when trying to run this:

当我试图运行时,我得到了错误:

<?php
require_once('includes/DbConnector.php');
$connector = new DbConnector();
$result = $connector->query('SELECT title,content FROM staff_vacancies ORDER BY ordering LIMIT 0,100');
// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){

echo $row['title'].'</h3>';
echo $row['content'];
}
?>

I have a linked file: DbConnector.php:

我有一个链接文件:DbConnector.php:

<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: DbConnector
// Purpose: Connect to a database, MySQL version
///////////////////////////////////////////////////////////////////////////////////////
require_once 'SystemComponent.php';

class DbConnector extends SystemComponent {

var $theQuery;
var $link;

//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector(){

    // Load settings from parent class
    $settings = SystemComponent::getSettings();

    // Get the main settings from the array we just loaded
    $host = $settings['dbhost'];
    $db = $settings['dbname'];
    $user = $settings['dbusername'];
    $pass = $settings['dbpassword'];

    //the settings
    $host = 'localhost';
    $db = 'xxx';
    $user = 'xxx';
    $pass = 'xxx';

    // Connect to the database
    $this->link = mysql_connect($host, $user, $pass);
    mysql_select_db($db);
    register_shutdown_function(array(&$this, 'close'));

}

//*** Function: query, Purpose: Execute a database query ***
function query($query) {
    $this->theQuery = $query;
    return mysql_query($query, $this->link);
}

//*** Function: getQuery, Purpose: Returns the last database query, for debugging ***
function getQuery() {
    return $this->theQuery;
}

//*** Function: getNumRows, Purpose: Return row count, MySQL version ***
function getNumRows($result) {
    return mysql_num_rows($result);
}

//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {
    return mysql_fetch_array($result);
}

//*** Function: close, Purpose: Close the connection ***
function close() {
    mysql_close($this->link);
}


}
?>

Does anyone know what the problem is?

有人知道问题出在哪里吗?

6 个解决方案

#1


17  

Your query must have a problem which is causing $result to be an invalid resource.

您的查询必须有一个导致$result为无效资源的问题。

Try checking for mysql_error() after the line on which you run your query.

尝试在运行查询的行之后检查mysql_error()。

Edit:

编辑:

In fact, I would alter your DBConnector class function query() to something like the following, so that an identifiable error is thrown when you have a bad query:

事实上,我将您的DBConnector类函数query()更改为以下内容,以便当您遇到错误查询时抛出可识别的错误:

function query($query) {
    $this->theQuery = $query;
    $queryId = mysql_query($query,$this->link);
    if (! $queryId) {
        throw new Exception(mysql_error().".  Query was:\n\n".$query."\n\nError number: ".mysql_errno();
    }
    return $queryId;
}

#2


2  

This error means your query failed. mysql_query() returns false if an error occurred, you are then passing false to mysql_fetch_array() which is triggering the error message.

这个错误意味着您的查询失败。如果出现错误,mysql_query()返回false,然后将false传递给mysql_fetch_array(),它将触发错误消息。

Your query could be failing due to a missing/wrong table or field. To see the detailed error, print out the result of mysql_error().

您的查询可能由于缺少/错误的表或字段而失败。要查看详细的错误,请打印mysql_error()的结果。

The mysql_* library is deprecated. It is recommended to upgrade to MySQLi or PDO.

不赞成使用mysql_*库。建议升级到MySQLi或PDO。

#3


1  

// Load settings from parent class
$settings = SystemComponent::getSettings();

// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];

//the settings
$host = 'localhost';
$db = 'xxx';
$user = 'xxx';
$pass = 'xxx';

Did you mean to reassign the connection vars? OR was that a few lines of stub code you forgot to take out? Or just an example to show what $settings contains?

你是想重新分配连接vars吗?还是你忘了取出几行存根代码?或者只是展示$settings包含哪些内容的示例?

#4


1  

Please provide the error from mysql_error(). Without that I can only guess... try escaping your field names?

请提供来自mysql_error()的错误。没有它,我只能猜测……试着逃避你的字段名?

$result = $connector->query('SELECT `title`,`content` FROM `staff_vacancies` ORDER BY `ordering` LIMIT 0,100');

#5


1  

Your query must have a problem which is causing $result to be an invalid resource.

您的查询必须有一个导致$result为无效资源的问题。

Use this

使用这个

<?php
require_once('includes/DbConnector.php');
$connector = new DbConnector();
$result = $connector->query('SELECT title,content FROM staff_vacancies ORDER BY ordering LIMIT 0,100');
// Get an array containing the results.
// Loop for each item in that array

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

echo $row['title'].'</h3>';
echo $row['content'];
}
}
?>

#6


1  

I find this in a post, to me solved my problem. Slds.

我在一个帖子里发现了这个问题,我解决了我的问题。道防线。

Yeah, Answer is simple, the query used is not a true result as it's a query inside of a getrow so to speak.. Here is the fix: Find all lines that look like this:

是的,答案很简单,使用的查询不是真实的结果,因为它是getrow中的查询。这里有一个修正:找到所有像这样的线:

mysql_fetch_array(mysql_query("...snip...";-) );

And just add a "@" in front of it so it looks like this:

在它前面加上一个“@”就像这样:

@mysql_fetch_array(mysql_query("...snip...";-) );

Then do the same thing for the following lines.. Code:

然后对以下几行做同样的事情。代码:

mysql_num_rows(mysql_query("...snip...";-) );

Perform the same steps as above by adding the "@" to it so it looks like this:

执行与上面相同的步骤,添加“@”,使其看起来如下:

@mysql_num_rows(mysql_query("...snip...";-) );

All this does it say "While doing xxx within yyy" Otherwise, it's dead due to missing result value. It's a PHP thing..

所有这些都说明“在yyy中做xxx”,否则就会因为缺少结果值而死亡。这是一个PHP的事. .

Works like a charm, took me 5 mins to rip the whole code apart and slap it all into Modernbill, Shares the same database and works perfectly for me.

我花了5分钟把所有的代码都拆下来,放进了Modernbill,共享同一个数据库,对我来说非常完美。

#1


17  

Your query must have a problem which is causing $result to be an invalid resource.

您的查询必须有一个导致$result为无效资源的问题。

Try checking for mysql_error() after the line on which you run your query.

尝试在运行查询的行之后检查mysql_error()。

Edit:

编辑:

In fact, I would alter your DBConnector class function query() to something like the following, so that an identifiable error is thrown when you have a bad query:

事实上,我将您的DBConnector类函数query()更改为以下内容,以便当您遇到错误查询时抛出可识别的错误:

function query($query) {
    $this->theQuery = $query;
    $queryId = mysql_query($query,$this->link);
    if (! $queryId) {
        throw new Exception(mysql_error().".  Query was:\n\n".$query."\n\nError number: ".mysql_errno();
    }
    return $queryId;
}

#2


2  

This error means your query failed. mysql_query() returns false if an error occurred, you are then passing false to mysql_fetch_array() which is triggering the error message.

这个错误意味着您的查询失败。如果出现错误,mysql_query()返回false,然后将false传递给mysql_fetch_array(),它将触发错误消息。

Your query could be failing due to a missing/wrong table or field. To see the detailed error, print out the result of mysql_error().

您的查询可能由于缺少/错误的表或字段而失败。要查看详细的错误,请打印mysql_error()的结果。

The mysql_* library is deprecated. It is recommended to upgrade to MySQLi or PDO.

不赞成使用mysql_*库。建议升级到MySQLi或PDO。

#3


1  

// Load settings from parent class
$settings = SystemComponent::getSettings();

// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];

//the settings
$host = 'localhost';
$db = 'xxx';
$user = 'xxx';
$pass = 'xxx';

Did you mean to reassign the connection vars? OR was that a few lines of stub code you forgot to take out? Or just an example to show what $settings contains?

你是想重新分配连接vars吗?还是你忘了取出几行存根代码?或者只是展示$settings包含哪些内容的示例?

#4


1  

Please provide the error from mysql_error(). Without that I can only guess... try escaping your field names?

请提供来自mysql_error()的错误。没有它,我只能猜测……试着逃避你的字段名?

$result = $connector->query('SELECT `title`,`content` FROM `staff_vacancies` ORDER BY `ordering` LIMIT 0,100');

#5


1  

Your query must have a problem which is causing $result to be an invalid resource.

您的查询必须有一个导致$result为无效资源的问题。

Use this

使用这个

<?php
require_once('includes/DbConnector.php');
$connector = new DbConnector();
$result = $connector->query('SELECT title,content FROM staff_vacancies ORDER BY ordering LIMIT 0,100');
// Get an array containing the results.
// Loop for each item in that array

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

echo $row['title'].'</h3>';
echo $row['content'];
}
}
?>

#6


1  

I find this in a post, to me solved my problem. Slds.

我在一个帖子里发现了这个问题,我解决了我的问题。道防线。

Yeah, Answer is simple, the query used is not a true result as it's a query inside of a getrow so to speak.. Here is the fix: Find all lines that look like this:

是的,答案很简单,使用的查询不是真实的结果,因为它是getrow中的查询。这里有一个修正:找到所有像这样的线:

mysql_fetch_array(mysql_query("...snip...";-) );

And just add a "@" in front of it so it looks like this:

在它前面加上一个“@”就像这样:

@mysql_fetch_array(mysql_query("...snip...";-) );

Then do the same thing for the following lines.. Code:

然后对以下几行做同样的事情。代码:

mysql_num_rows(mysql_query("...snip...";-) );

Perform the same steps as above by adding the "@" to it so it looks like this:

执行与上面相同的步骤,添加“@”,使其看起来如下:

@mysql_num_rows(mysql_query("...snip...";-) );

All this does it say "While doing xxx within yyy" Otherwise, it's dead due to missing result value. It's a PHP thing..

所有这些都说明“在yyy中做xxx”,否则就会因为缺少结果值而死亡。这是一个PHP的事. .

Works like a charm, took me 5 mins to rip the whole code apart and slap it all into Modernbill, Shares the same database and works perfectly for me.

我花了5分钟把所有的代码都拆下来,放进了Modernbill,共享同一个数据库,对我来说非常完美。