Yes I was in a bit of a rush when I read your problem before because I was on my way out the door. Now that I have had a chance to look at your problem again it occurred to me that mysql will throw the blah blah blah resource error when there is a syntax error in your query and then attempting to fetch the result set, meaning that because the query was not successful, therefore mysql_fetch_row() is not a valid resource. I had for some reason thought that there might have been a typo there with the setting of the mysql resource... but I've got it now!
I find it helpful to make my queries in a user-defined function, which helps reduce some redundancy in code...
Code:
function query($query, $link) {
$result = mysql_query($query, $link);
if (empty($result)) {
echo mysql_error().": ";
echo mysql_errno()."\n";
return false;
}
return $result;
}
Wrapping your query calls in a function makes sense for two key reasons...
1. Redundant tasks are best enclosed within functions or classes, meaning less time is spend on development!
2. Error checking should always be included in any task to create the best scenario for quickly and efficiently troubleshooting application failures.
How are you going to know what's going wrong and where if you don't plan for it?
Personally I only use one resource connection and store the value in a super-global array $GLOBALS["link"] -- I like this approach because then I don't have to pass it as an argument to my user-defined functions and classes. I like to have only one connection at the beginning of my whole application and then reuse that connection throughout the life of the script, because database connections are resource intensive, the fewer the better! Well when we're talking about connecting to the same database. I understand that PHP can have many connections to many databases --- but the more connections either way, the more resources consumed. As far as the super-global method is concerned, if version portability isn't an issue then you might consider this approach.
I have to agree with Nik (Its hard to ever disagree with Nik! He's right 99.9% of the time ;))... I would output the value of the variable to help track down where things are going awry.
Always always always plan a project with its failure in mind. If a variable's value is critical create a routine to check that its value absolutely exists and is what you intend it to be... isset() and empty() are IMO two of the most useful functions that exist. isset() checks for a variable's existence, empty() checks for null value... a subtle but very important difference.
Code:
#Don't do this...
if ($variable) {
# Execute code relying on the existence of $variable
} else {
echo "Error: failure, \$variable not set at... ";
}
#Do this...
if (isset($variable)) && !empty($variable)) {
# If variable exists and isn't null
# Execute code relying on the existence of $variable
} else {
echo "Error: \$variable not set at... <br />\n";
}
I would even take this one step further and wrap my error reporting within a function to control error output using a boolean variable or constant. Error output is very good in development, but unprofessional in a live production site. So you might set it up so that it outputs to the screen if ERROR_BOOLEAN is true and to a text file log if ERROR_BOOLEAN is false.
Also consider the error text from PHP's native error reporting...
Warning: Supplied argument is not a valid MySQL result resource in /user1/********/www/admin/edit_musictitles.php on line 265
Line 265 will correspond to the function call mysql_fetch_row()... which indicated an error with the argument being passed to it, which will in turn mean one of two things, a typo in the result variable or an error in SQL syntax... if you are getting that error when trying to fetch a result set then you can pretty much lay money on that being exactly the issue every time. (Just some words of wisdom for the future there!)
IMO, you can never have too much error reporting! Its certainly saved me from pulling out my hair several times. I'm glad to have been of help~!
: )
Rich
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::