Page 1 of 1

Dumb Question

Posted: Sat Jan 19, 2008 10:34 am
by RobertBr
Part of some code, this part in fact

Code: Select all

 
  foreach ($dieresults as $key => $value){
   $sql = "SELECT * FROM images WHERE prefer =1 AND coin = (SELECT coinID FROM `main` WHERE `".$table."` = '".$value."' LIMIT 0 , 1) LIMIT 0 , 1";
   $imageref = mysql_query($sql) or die('Query failed: ' . mysql_error()); 
    echo ("<p>Die Number:".$value." ");
    echo ($imageref["revimage"]);
   }
  }
 
simply isn't working. All the rest of the code is and this part doesn't produce an error (either SQL or PHP). It returns Die Number: and the right $value to the screen, but then nothing for $imageref["revimage"] as if the query had not returned anything for that entry. Except that I can make it echo the whole sql statement to the screen and then plug that directly into MySQL and it works fine, returning exactly the record I expected.

I'm not getting anywhere with this one, can someone suggest what I've overlooked.

Rhanks,

Robert

Re: Dumb Question

Posted: Sat Jan 19, 2008 10:48 am
by Christopher
What is $table?

Re: Dumb Question

Posted: Sat Jan 19, 2008 2:51 pm
by RobertBr
Sorry,

a bit more context. The query is selecting an image of either the obverse or reverse of a coin, the dies for those two sides are held in two different tables one for the obverse and one for the reverse, $table just contains the name of that table and thus tells the query which side the user is interested in. While $value is the particular die that you are looking for an example of.

Re: Dumb Question

Posted: Sat Jan 19, 2008 5:10 pm
by Mordred
mysql_query() does not work in that way.
check mysql_fetch_assoc() and the like.

Re: Dumb Question

Posted: Sat Jan 19, 2008 5:55 pm
by Christopher
Well spotted Morderd (and -1 for me ;)). It should be:

Code: Select all

 
  foreach ($dieresults as $key => $value){
   $sql = "SELECT * FROM images WHERE prefer =1 AND coin = (SELECT coinID FROM `main` WHERE `".$table."` = '".$value."' LIMIT 0 , 1) LIMIT 0 , 1";
   $result= mysql_query($sql) or die('Query failed: ' . mysql_error());
   $imageref = mysql_fetch_assoc($result);
    echo ("<p>Die Number:".$value." ");
    echo ($imageref["revimage"]);
   }
  }
 

Re: Dumb Question

Posted: Sun Jan 20, 2008 4:37 am
by RobertBr
Thank you, it was dumb that I couldn't see it because I had already done it right in that code.