gpt4 book ai didi

php - 警告 : mysql_fetch_array(): supplied argument is not a valid MySQL result

转载 作者:行者123 更新时间:2023-11-29 15:13:19 25 4
gpt4 key购买 nike

尝试运行此命令时出现错误:

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

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


}
?>

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

最佳答案

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

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

编辑:

事实上,我会将您的 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;
}

关于php - 警告 : mysql_fetch_array(): supplied argument is not a valid MySQL result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59889712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com