Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-23-2002, 02:09 PM   PM User | #1
Edward101
New to the CF scene

 
Join Date: Aug 2002
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Edward101 is an unknown quantity at this point
Unhappy PHP / MySQL variable problem

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...
Edward101 is offline   Reply With Quote
Old 08-23-2002, 03:29 PM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,896
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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...

PHP Code:
<?
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());
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 08-23-2002, 03:49 PM   PM User | #3
Edward101
New to the CF scene

 
Join Date: Aug 2002
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Edward101 is an unknown quantity at this point
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?
Edward101 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:13 PM.


Advertisement
Log in to turn off these ads.