noname_clark Posted August 10, 2008 Share Posted August 10, 2008 hello, i am trying to have a sql statement grab a number from a table called 'photosunday' and then use that number in a sql for another table called 'link'. i have a table called photosunday with: counter hidden and a few other un-importand stuff for this.... 5 59 4 58 3 57 2 56 1 55 and a table called link with: counter hidden and a few other un-importand stuff for this.... 1 1 thru thru 700 59 heres the code: <?php $conn=mysql_connect("db","usr","pass"); mysql_select_db("usr",$conn); // heres the first sql statement: $sql = 'SELECT * FROM `photosunday` ORDER BY `counter` DESC LIMIT 1'; $result=mysql_query($sql,$conn) or die(mysql_error()); while ($newArray=mysql_fetch_array($result)) { // this gets the number i need, in this case, it is 59. $hidden=$newArray['hidden']; } // so heres the second sql statement, and theres the variable $hidden $sql = 'SELECT * FROM `link` WHERE `theme`= "$hidden" ORDER BY `counter`'; $result=mysql_query($sql,$conn) or die(mysql_error()); echo $hidden; echo "<ol>"; while ($newArray=mysql_fetch_array($result)) { $name=$newArray['name']; $url=$newArray['url']; echo "<li><a href=\"$url\" target=\"_blank\">$name</a></li>"; } echo "</ol>"; ?> i can put a few echo $hidden; so i know that its the right number, and i know that there are atleast 12 records in 'link' where `hidden` = 59 so if someone could tell me the correct quotation for the hidden = $hidden in the $sql = 'SELECT * FROM `link` WHERE `theme`= "$hidden" ORDER BY `counter`'; that would be greatly appriciated thanks in advance! Link to comment https://forums.phpfreaks.com/topic/119049-solved-variable-in-sql-statement/ Share on other sites More sharing options...
ratcateme Posted August 10, 2008 Share Posted August 10, 2008 because you are using single quotes the vars are not changed to there values try this $sql = "SELECT * FROM `link` WHERE `theme`= '{$hidden}' ORDER BY `counter`"; Scott. Link to comment https://forums.phpfreaks.com/topic/119049-solved-variable-in-sql-statement/#findComment-613024 Share on other sites More sharing options...
noname_clark Posted August 10, 2008 Author Share Posted August 10, 2008 thanks! that worked. thanks so much -Noah Link to comment https://forums.phpfreaks.com/topic/119049-solved-variable-in-sql-statement/#findComment-613025 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.