Page 1 of 1

PHP / MySQL variable problem

Posted: Fri Aug 23, 2002 7:42 am
by Edward101
Hi,

having a real problem with a bit of code.

I've passed 10 variables to a PHP script:

$Answer1
$Answer2
....
$Answer10

Each variable contains a code, which relates to a field called "code", ina record in a MySQL database. The other field in each record is called "body".

I'm trying to query out the data in the "body" field, for each record that matches the data in one of the $Answer variables. So I've got the following code so far:

for ($n = 0; $n < 10; $n++) {
$answervar = "$answer$n";
$sqlquery = "SELECT body FROM $table WHERE code = {$$answervar}";
$result = mysql_query($sqlquery);
while($row = mysql_fetch_object($result)) {
print $row->body; }
}

So I'm trying to move through each variable, query the table, print the value, and move onto the next one....only for the code above I'm getting the following error:

Warning: Supplied argument is not a valid MySQL result resource in /home/sites/site102/web/arraytest.php on line 30

I've tried fiddling with everything but I just cant get it to work...can anyone help? :(

Thanks...

Posted: Fri Aug 23, 2002 8:07 am
by nielsene
I think you want

Code: Select all

$answervar = "Answer$n"; // Ie the word Answer not the variable $answer
...
sqlquery = "SELECT body FROM $table WHERE code ='&#123;$$answervar&#125;';";
// added quotes around variable, $table is defined, yes?

Posted: Fri Aug 23, 2002 8:10 am
by llimllib
I'll wager that 'line 30' is the while loop. Your problem is that mysql_query returned an error which you have not checked. Try:

Code: Select all

$result = mysql_query($sqlquery) or die(mysql_error());
and post up what the error message is.

hmmm....

Posted: Fri Aug 23, 2002 8:51 am
by Edward101
Thanks folx...tried both of these suggestions tho, and while I'm no longer getting an error, I'm now just getting an empty page... :( any more suggestions?

Posted: Fri Aug 23, 2002 8:57 am
by llimllib
your empty page signifies a compile error, and you probably have display_errors turned off. Do a phpinfo() and look for error_log. If you have one, look in it and it'll tell you the compiler error, otherwise go into your php.ini and set log_errors to true and error_log to some file you'd like it to be in.

:( :( :(

Posted: Fri Aug 23, 2002 9:20 am
by Edward101
Damn - I know it's not very helpful, but my hosting company runs the admin side of the servers, and they are closed until Tues next week coz of the bank holiday :(

I've had a look around the server, but I can't find anything. Is there any other way?

Posted: Fri Aug 23, 2002 9:55 am
by llimllib
debug the old-fashioned way - start commenting out lines until you figure out which one's causing the problem, then find out what's wrong with that line. Post it up here if you have trouble.