tags:

views:

76

answers:

2

I'm trying to set a simple cron script to do some database updating, and I'm pretty worthless with MySQL without ActiveRecord (I use CodeIgniter). I keep getting the error message,

mysql_fetch_object(): supplied argument is not a valid MySQL result resource

with the following code:

mysql_connect("localhost", "user", "pass") or die(mysql_error()); 
mysql_select_db("my_db") or die(mysql_error()); 
$query = "select visit_e_id, visit_e_type from visits";
$result = mysql_query($query)
or die("Query failed: ".mysql_error()." Actual query: ".$query);

while($row=mysql_fetch_object($result))
{
     ....
}

Like I said, I'm not that great with straight PHP and MySQL (would appreciate any advice on how to include some sort of framework or ActiveRecords that could be used as part of a cron job). Any thoughts?

A: 

What happens if you assign mysql_connect as a variable and pass it in as a link identifier as a second parameter to the mysql_select_db and query functions?

$db = mysql_connect("localhost", "user", "pass") or die(mysql_error()); 
// Check to see if valid connection
var_dump($db);

mysql_select_db("my_db", $db) or die(mysql_error()); 
$query = "select visit_e_id, visit_e_type from visits";
$result = mysql_query($query, $db)
or die("Query failed: ".mysql_error()." Actual query: ".$query);

while($row=mysql_fetch_object($result))
{
     ....
}  

Also ensure that you query syntax is correct and doesn't have misspelling or typos

Stoosh
+1  A: 

This error is pretty simple and self-explanatory - a $result variable has unexpected type.
Thus, you have to so some debugging. Add var_dump($result); before and inside loop and study the output.

Col. Shrapnel