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

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

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


<?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:


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

}


有谁知道问题是什么?


吃鸡游戏
浏览 952回答 3
3回答

RISEBY

您的查询必须有一个问题,导致$ result成为无效资源。尝试在运行查询的行之后检查mysql_error()。编辑:实际上,我会将您的DBConnector类函数query()更改为类似以下内容,以便在查询错误时抛出可识别的错误:function&nbsp;query($query)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$this->theQuery&nbsp;=&nbsp;$query; &nbsp;&nbsp;&nbsp;&nbsp;$queryId&nbsp;=&nbsp;mysql_query($query,$this->link); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!&nbsp;$queryId)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;Exception(mysql_error().".&nbsp;&nbsp;Query&nbsp;was:\n\n".$query."\n\nError&nbsp;number:&nbsp;".mysql_errno(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$queryId;}

蓝山帝景

此错误表示您的查询失败。mysql_query()如果发生错误则返回false,然后将false传递给mysql_fetch_array()触发错误消息的false&nbsp;。由于缺少/错误的表或字段,您的查询可能会失败。要查看详细错误,请打印出mysql_error()的结果。该mysql_*库已弃用。建议升级到MySQLiPDO或PDO。

侃侃无极

// 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';你的意思是重新分配连接变量吗?或者是你忘记取出几行存根代码?或者只是一个示例来显示$ settings包含的内容?
打开App,查看更多内容
随时随地看视频慕课网APP