LeGrande
LeGrande

Reputation: 230

Running a query while within a mysql_fetch_object while loop causing error

I'm runing a query to pull some movie data from a source table to import into a new database structure. I'm then looping through the results using mysql_fetch_object and parsing data, calling external functions, etc to add the genres,actors,etc... to other tables in the database.

In one of my functions that I'm calling makes a query to a different table within the same database. I'm then getting a "not a valid MySQL-Link resource" on that query.

Is there anything that would cause issues doing this?

Here's some code on how the error is occuring:

function doThis($thing) { 
   mysql_query("SELECT derp FROM herp WHERE sherp = $thing",$db)or die(mysql_error()); 
}

$query = mysql_query("SELECT foo FROM bar",$db);
while($row = mysql_fetch_object($query)) {
   doThis($row->awesome);
}

This is essentially what I'm doing and I'm getting an error and don't know why.

Something of note may be that I'm connecting to 2 databases during this process. The database I'm pulling data from and the database I'm importing into.

Thanks!

Upvotes: 0

Views: 534

Answers (1)

gnxtech3
gnxtech3

Reputation: 772

2 things: you haven't declared $db as a global variable within your function (so it's undeclared, causing the error you're seeing). Add the following:

global $db;

Second, remove the function keyword from inside your while loop. That's likely to cause php to biff.

Upvotes: 2

Related Questions