Question

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

was the error I got when I use the below class, even though the class works PERFECTLY with my old webhost. Here's my new hosts php info: 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>";
 }
}
Was it helpful?

Solution

This is because mysql_query() will return FALSE if an error occured, instead of returning a result resource. You can check the error by calling mysql_error(), as shown here:

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

Based on the error message, you can find out what the real problem is.

OTHER TIPS

You should really test to see if $result is not false before using it with mysql_fetch_array. The error you're receiving is indicative that the query itself failed.

Have you configured your database with your new host? (do all the tables exist?)

As Mark said above, you really should check your result before trying mysql_fetch_array on a result set, and verify that all tables actually exist.

Without knowing how your original server was set up, I can only guess, but it may also be that your old server was set up to not display warnings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top