Jump to content

[SOLVED] Helpp!!


Jacko623

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.