警告:mysql_fetch_array():提供的参数不是有效的 MySQL 结果 [英] Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

查看:26
本文介绍了警告:mysql_fetch_array():提供的参数不是有效的 MySQL 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试运行时遇到错误:

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'];
}
?>

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

I have a linked file: 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?

推荐答案

您的查询一定有问题导致 $result 成为无效资源.

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

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

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

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

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:

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

这篇关于警告:mysql_fetch_array():提供的参数不是有效的 MySQL 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆