Jacko623 Posted February 18, 2007 Share Posted February 18, 2007 The following code randomly chooses one bar and a city_id from a database, then looks up the city name, using the city_id in the second database. This returns no errors, however, the last two variables do not return information. <?php //ommitted connect strings $ConDB=mysql_connect($hostname,$username,$password); $DB=mysql_select_db($dbname,$ConDB); //Query Information $query = "SELECT bar_name,bar_descrip,city FROM partynj_bar_details ORDER by Rand() limit 0,1"; $result = mysql_query($query); //print information while(list($bar_name,$bar_descrip) = mysql_fetch_array($result)) { ?> <a href="../bars/?<?=stripslashes($bar_name)."|".$id?>&act=all"><?=stripslashes($bar_name)?></a><br> <? echo stripslashes("Name : $bar_name <br>"); echo stripslashes("Description : $bar_descrip <br><br>"); } //HELP HERE $queryT = "SELECT city FROM partynj_citylist WHERE id='$city'"; $resultT = mysql_query($queryT); // echo stripslashes($bar_name)?> [b]is located in [/b] <?=stripslashes($resultT); ?> The //HELP HERE part of the script returns the following: is located in Resource id #3 What do you all think? Link to comment https://forums.phpfreaks.com/topic/39029-solved-helpp/ Share on other sites More sharing options...
trq Posted February 18, 2007 Share Posted February 18, 2007 mysql_query returns a result resource, you appear to be expecting a string. Try... if ($resultT = mysql_query($queryT) && mysql_num_rows($queryT)) { $row = mysql_fetch_assoc($resultT); echo stripslashes($bar_name)?> [b]is located in [/b] <?=stripslashes($row['city']); } Link to comment https://forums.phpfreaks.com/topic/39029-solved-helpp/#findComment-187937 Share on other sites More sharing options...
kenrbnsn Posted February 18, 2007 Share Posted February 18, 2007 The mysql_query() function returns a pointer to the returned data set. You need to use one of the mysql fetch functions to actually get the data. <?php //ommitted connect strings $ConDB=mysql_connect($hostname,$username,$password); $DB=mysql_select_db($dbname,$ConDB); //Query Information $query = "SELECT bar_name,bar_descrip,city FROM partynj_bar_details ORDER by Rand() limit 0,1"; $result = mysql_query($query); //print information while(list($bar_name,$bar_descrip) = mysql_fetch_assoc($result)) { ?> <a href="../bars/?<?=stripslashes($bar_name)."|".$id?>&act=all"><?=stripslashes($bar_name)?></a><br> <?php echo stripslashes("Name : $bar_name <br>"); echo stripslashes("Description : $bar_descrip <br><br>"); } //HELP HERE $queryT = "SELECT city FROM partynj_citylist WHERE id='$city'"; $resultT = mysql_query($queryT); $rw = mysql_fetch_assoc($resultT); // echo stripslashes($bar_name) . ' <b>is located in</b> ' .stripslashes($rw['city']); ?> Ken Link to comment https://forums.phpfreaks.com/topic/39029-solved-helpp/#findComment-187938 Share on other sites More sharing options...
Jacko623 Posted February 18, 2007 Author Share Posted February 18, 2007 Hey Ken, I'm still having the same result of 'is located in' I am missing the bar_name variable and the city variable. Any other advice? Why is the bar_name variable not being returned btw... it is being returned in the firs thalf of the query string. Link to comment https://forums.phpfreaks.com/topic/39029-solved-helpp/#findComment-187951 Share on other sites More sharing options...
kenrbnsn Posted February 18, 2007 Share Posted February 18, 2007 Let's clean up your code a litte. Since you're only retrieving one record with the first select, you don't need the while loop, in fact, that might be why you're loosing the value of $bar_name: <?php //ommitted connect strings $ConDB=mysql_connect($hostname,$username,$password); $DB=mysql_select_db($dbname,$ConDB); //Query Information $query = "SELECT bar_name,bar_descrip,city FROM partynj_bar_details ORDER by Rand() limit 0,1"; $result = mysql_query($query); //print information list($bar_name,$bar_descrip) = mysql_fetch_assoc($result); echo '<a href="../bars/?' . stripslashes($bar_name). '|'. $id . '&act=all">' . stripslashes($bar_name) . '</a><br>'; echo 'Name: ' . stripslashes($bar_name) . ' <br>'; echo 'Description: . ' stripslashes($bar_descrip) . ' <br><br>'; //HELP HERE $queryT = "SELECT city FROM partynj_citylist WHERE id='$city'"; $resultT = mysql_query($queryT) or die("Problem with the query <pre>$queryT</pre><br>" . mysql_error()); if (mysql_num_rows($resultT) == 1) { $rw = mysql_fetch_assoc($resultT); echo stripslashes($bar_name) . ' <b>is located in</b> ' .stripslashes($rw['city']); } else echo 'There is a problem finding the city where ' . stripslashes($bar_name) . ' is located'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/39029-solved-helpp/#findComment-187970 Share on other sites More sharing options...
Jacko623 Posted February 18, 2007 Author Share Posted February 18, 2007 Thanks again for the help Ken, but the script is just not working. It returns the else statement time after time. I cannot figure out what the hell is going on. What do you think could be the problem? the script is pretty straight forward. Link to comment https://forums.phpfreaks.com/topic/39029-solved-helpp/#findComment-187979 Share on other sites More sharing options...
kenrbnsn Posted February 18, 2007 Share Posted February 18, 2007 You never assign a value to the variable "$city". Change this line: <?php list($bar_name,$bar_descrip) = mysql_fetch_assoc($result); ?> to <?php list($bar_name,$bar_descrip,$city) = mysql_fetch_assoc($result); ?> Ken Link to comment https://forums.phpfreaks.com/topic/39029-solved-helpp/#findComment-187981 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.