警告:mysql_fetch_array()要求参数1为资源,中给出的为null
我得到以下错误:警告:mysql_fetch_array()要求参数1为资源,中给出的为null,mysql,Mysql,我得到以下错误: Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/pantyho1/public_html/affiliates/classes/XpDb.php on line 185 这是我的密码: class XpDatabase { var $mDbhost; var $mDbuser; var $mDbpwd; var $mDbname; /** * Conne
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/pantyho1/public_html/affiliates/classes/XpDb.php on line 185
这是我的密码:
class XpDatabase
{
var $mDbhost;
var $mDbuser;
var $mDbpwd;
var $mDbname;
/**
* Connects to database
*/
function connect()
{
$link = mysql_connect($this->mDbhost, $this->mDbuser, $this->mDbpwd);
if (!$link)
{
$error = 'Could not connect: '.mysql_error();
$this->printError($error);
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db($this->mDbname))
{
$error = 'Can\'t use database : ' . mysql_error();
$this->printError($error);
die ('Can\'t use database : ' . mysql_error());
}
}
/**
* Close connection to database
*
* @param $aConnection connection
*
* return bool
*/
function close($aConnection)
{
return mysql_close($aConnection);
}
/**
* Executes sql query
*
* @param str $aSql sql query
*
* @return bool
*/
function query($aSql)
{
return mysql_query($aSql);
}
/**
* Returns row of elements
*
* @param str $aSql sql query
*
* @return arr
*/
function getRow($aSql)
{
$res = $this->query($aSql);
$out = mysql_fetch_assoc($res);
return $out;
}
/**
* Returns array of rows
*
* @param str $aSql sql query
*
* @return arr
*/
function getAll($aSql)
{
$out = Array();
$res = $this->query($aSql);
while($temp = mysql_fetch_assoc($res))
{
$out[] = $temp;
}
return $out;
}
/**
* Returns recordset as associative array where the key is the first field
*
* @param str $aSql sql query
*
* @return arr
*/
function &getAssoc($aSql)
{
$out = Array ();
$res =& $this->query($aSql);
while ($temp = mysql_fetch_assoc($res))
{
$key = array_shift($temp);
$out[$key][] = $temp;
}
return $out;
}
/**
* Returns one element
*
* @param str $aSql sql query
*
* @return int
*/
function getOne($aSql)
{
$res = $this->query($aSql);
$row = mysql_fetch_row($res);
$out = ($row ) ? $row[0] : '';
return $out;
}
/**
* Returns array of tables
*
* @return arr
*/
function getTables()
{
$out = Array();
$sql = "SHOW TABLES FROM {$this->mDbname}";
$res = $this->query($sql);
while ($row = mysql_fetch_row($res))
{
$out[] = $row[0];
}
return $out;
}
/**
* Prints out block with error
*/
function printError($aError)
{
echo $aError;
}
/**
* Returns recordset as associative array where the key is the first field
*
* @param str $aSql sql query
*
* @return arr
*/
function getKeyValue($aSql)
{
$out = Array ();
$res = $this->query($aSql);
line 185 while($row = mysql_fetch_array($result))
{
$out[$temp[0]] = $temp[1];
}
return $out;
}
}
我可以从您发布的代码中看到一些问题
getKeyValue()
函数更改中:
$res = $this->query($aSql);
到
“line 185”
,这几乎肯定是打字错误或剪切/粘贴错误connect()
函数创建的连接资源(例如,在同时打开两个数据库连接的情况下),请在connect()
函数的末尾添加此行:
. 它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO。(你很幸运使用了包装器,所以切换应该很容易!)不是同一个问题。可能是类似的错误消息,但原因不同。这里的问题是变量名输入错误。“复制”中的问题是一个失败的查询。现在我得到以下错误:解析错误:语法错误,意外的“return”(T_return),预期为“;”或者“对不起,第185行代码如下,$temp=mysql\u fetch\u row($res))
$result = $this->query($aSql);
return $link;