[Phpwm] How to get a single string from different queries?

Elliot Smith elliot at townx.org
Wed Apr 19 14:56:58 BST 2006


Hello Ray,

When you call mysql_query, what you get back is a result set (when doing 
a SELECT) or true/false (for other types of query). That's why you get 
"Resource id #3" echoed (that's how PHP converts the result set into a 
string). What your code is trying to do is join two result sets as if 
they were a string. What you need to do is get the values you want out 
of the result sets, then append them to each other. Something like:

<?php
mysql_connect("host", "user", "pass") or die(mysql_error());
mysql_select_db("realty") or die(mysql_error());

$results = mysql_query("SELECT listingsdbelements_field_value FROM 
en_listingsdbelements WHERE listingsdbelements_field_name = 'address'") 
or die(mysql_error());

$row = mysql_fetch_array( $results );
$address = $row['listingsdbelements_field_value'];

$results = mysql_query("SELECT listingsdbelements_field_value FROM 
en_listingsdbelements WHERE listingsdbelements_field_name = 'city'") or 
die(mysql_error());

$row = mysql_fetch_array( $results );
$city = $row['listingsdbelements_field_value'];

$full_address=$city.', '.$address;
echo $full_address;
?>

By the way, your database could be in need of some normalisation, by the 
looks of things. Any reason why you don't just have a table with 'city' 
and 'address' fields?

(You could probably also get both fields in a single query, as a 
previous post suggested, but I kept your structure.)

Elliot





Ray Masa wrote:

> Hi all,
>
> I am trying to join several database queries into a single string, but 
> have not been able to do so, any suggestions?
>
> Here is the PHP code:
>
> <?php
>
> mysql_connect("host", "user", "pass") or die(mysql_error());
> mysql_select_db("realty") or die(mysql_error());
>
> $address = mysql_query("SELECT listingsdbelements_field_value FROM 
> en_listingsdbelements WHERE listingsdbelements_field_name = 
> 'address'") or die(mysql_error());
> while($row1 = mysql_fetch_array( $address )) {
> // check to see if query is being fetched
> echo $row1['listingsdbelements_field_value']. "<br />";
> }
>
> $city = mysql_query("SELECT listingsdbelements_field_value FROM 
> en_listingsdbelements WHERE listingsdbelements_field_name = 'city'") 
> or die(mysql_error());
> while($row2 = mysql_fetch_array( $city )) {
> // check to see if query is being fetched
> echo $row2['listingsdbelements_field_value']. "<br />";
> }
>
> // join the above query in one string
>
> $full_address=$city.', '.$address;
>
>
> echo $full_address;
>
> ?>
>
>
> The above give me the following output:
>
> 1200 Atwater Ave.
> 1240 Rue Drummond
> Montreal
> Westmount
> H3G 1V7
> H3A 1Y1
> Resource id #3, Resource id #4
>
>
> The individual queries are returning and echoing the results 
> correctly, but the last string ($full_address) is giving me error 
> (Resource id #3, Resource id #4).
>
> Any ideas on how to solve this?
>
> Thanks,
>
> Ray
>
>
>
> _______________________________________________
> Phpwm mailing list
> Phpwm at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/phpwm





More information about the Phpwm mailing list