...

PHP / MySQL variable problem

Edward101
08-23-2002, 02:09 PM
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...

firepages
08-23-2002, 03:29 PM
variables are case sensitive in PHP $Answer and $answer are 2 different variables

if you ever have hassles with queries echo them out so you can see what in fact you are passing to the db...


<?
for ($n = 1; $n <= 5; $n++) {
$answervar = ${"Answer".$n};
echo "SELECT body FROM $table WHERE code = '$answervar'";
}
?>


(if code is an int drop the single quotes around $answervar)

+ use mysql_error() after your queries for better error messages...


$result = mysql_query($sqlquery)or die(mysql_error());

Edward101
08-23-2002, 03:49 PM
Cool - thanks, yeah, you were right, there was a case mismatch there...

so up to


<?
for ($n = 1; $n <= 5; $n++) {
$answervar = ${"Answer".$n};
echo "SELECT body FROM $table WHERE code = '$answervar'";
}
?>

I'm getting a loop of correct select statements...now I can't seem to get the things to "Print"... :/

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


doesn't seem to work. NOW what am I doing wrong? :(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum