PHP / MySQL variable problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Edward101
Forum Newbie
Posts: 4
Joined: Fri Aug 23, 2002 7:42 am
Location: london

PHP / MySQL variable problem

Post 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...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Edward101
Forum Newbie
Posts: 4
Joined: Fri Aug 23, 2002 7:42 am
Location: london

hmmm....

Post 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?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Edward101
Forum Newbie
Posts: 4
Joined: Fri Aug 23, 2002 7:42 am
Location: london

:( :( :(

Post 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?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Post Reply