0
Warning:mysql_fetch_array(): supplied argument is not a valid MySQL result resource in **/home/davzyco1/public_html/notes/functions.php** on line 43

是我在使用以下课程时遇到的错误,即使该课程与我的旧网络主机完美配合。这是我的新主机 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>";
 }
}
4

3 回答 3

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

根据错误消息,您可以找出真正的问题所在。

于 2010-06-22T02:19:05.733 回答
0

在将$result它与mysql_fetch_array. 您收到的错误表明查询本身失败。

您是否使用新主机配置了数据库?(所有表都存在吗?)

于 2010-06-22T02:18:14.067 回答
0

正如马克上面所说,你真的应该在对结果集尝试 mysql_fetch_array 之前检查你的结果,并验证所有表确实存在。

在不知道您的原始服务器是如何设置的情况下,我只能猜测,但也可能是您的旧服务器设置为不显示警告

于 2010-06-22T02:21:40.110 回答