Warning:mysql_fetch_array(): supplied argument is not a valid MySQL result resource in **/home/davzyco1/public_html/notes/functions.php** on line 43
是我使用下面的类时遇到的错误,即使该类与我的旧webhost完美配合。这是我的新主机php信息:http://davzy.com/notes/php.php
class mysqlDb
{
public $con;
public $debug;
function __construct($host,$username,$password,$database)
{
$this->con = mysql_connect($host,$username,$password);
if (!$this->con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $this->con);
}
function kill()
{
mysql_close($this->con);
}
function debugOn()
{
$this->debug = true;
}
function debugOff()
{
$this->debug = false;
}
function select($query,&$array)
{
$c = 0;
$result = mysql_query("SELECT ".$query);
if($this->debug == true)
echo "SELECT ".$query;
while($row = mysql_fetch_array($result))
{
foreach($row as $id => $value)
{
$array[$c][$id] = $value;
}
$c++;
}
}
function update($update, $where,$array)
{
foreach($array as $id => $value)
{
mysql_query("UPDATE {$update} SET {$id} = '{$value}'
WHERE {$where}");
if($this->debug == true)
echo "UPDATE {$update} SET {$id} = '{$value}'
WHERE {$where}<br><br>";
}
}
function updateModern($update, $where,$array)
{
foreach($array as $id => $value)
{
mysql_query("UPDATE {$update} SET `{$id}` = '{$value}'
WHERE {$where}");
if($this->debug == true)
echo "UPDATE {$update} SET {$id} = '{$value}'
WHERE {$where}<br>";
}
}
function delete($t, $w)
{
mysql_query("DELETE FROM `{$t}` WHERE {$w}");
if($this->debug == true)
echo "DELETE FROM `{$t}` WHERE {$w}<br><br>";
}
function insert($where, $array)
{
$sql = "INSERT INTO `{$where}` (";
$sql2 = " VALUES (";
foreach($array as $id => $value){
$sql .= "`{$id}`, ";
$sql2 .= "'{$value}', ";
}
mysql_query(str_replace(', )',')',$sql.")") . str_replace(', )',')',$sql2.");"));
if($this->debug == true)
echo str_replace(', )',')',$sql.")") . str_replace(', )',')',$sql2.");")."<br><br>";
}
}
答案 0 :(得分:2)
这是因为如果发生错误,mysql_query()
将返回FALSE,而不是返回结果资源。您可以通过调用mysql_error()
来检查错误,如下所示:
function select($query,&$array)
{
$c = 0;
$result = mysql_query("SELECT ".$query);
if($this->debug == true)
echo "SELECT ".$query;
if (!$result) {
// an error occured, let's see what it was
die(mysql_error());
}
while($row = mysql_fetch_array($result))
{
foreach($row as $id => $value)
{
$array[$c][$id] = $value;
}
$c++;
}
}
根据错误消息,您可以找出真正的问题。
答案 1 :(得分:0)
在将$result
与mysql_fetch_array
一起使用之前,您应该先测试{{1}}是否为假。您收到的错误表明查询本身失败。
您是否使用新主机配置了数据库? (所有表都存在吗?)
答案 2 :(得分:0)
正如Mark上面所说,你真的应该在对结果集上尝试mysql_fetch_array之前检查你的结果,并验证所有表是否确实存在。
在不知道原始服务器是如何设置的情况下,我只能猜测,但也可能是您的旧服务器设置为display warnings。