Dumb Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RobertBr
Forum Newbie
Posts: 11
Joined: Fri Oct 20, 2006 1:28 pm

Dumb Question

Post 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
Last edited by RobertBr on Sat Jan 19, 2008 2:54 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Dumb Question

Post by Christopher »

What is $table?
(#10850)
RobertBr
Forum Newbie
Posts: 11
Joined: Fri Oct 20, 2006 1:28 pm

Re: Dumb Question

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Dumb Question

Post by Mordred »

mysql_query() does not work in that way.
check mysql_fetch_assoc() and the like.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Dumb Question

Post 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"]);
   }
  }
 
(#10850)
RobertBr
Forum Newbie
Posts: 11
Joined: Fri Oct 20, 2006 1:28 pm

Re: Dumb Question

Post by RobertBr »

Thank you, it was dumb that I couldn't see it because I had already done it right in that code.
Post Reply