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

Phil Beynon phil at infolinkelectronics.co.uk
Wed Apr 19 16:08:20 BST 2006


> 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

Ray,

Hope this makes sense........

When you select the address and the city fields seperately like this there
is absolutely no reason for the data to match up in the query arrays or even
contain the same number of elements, hence the resource id errors.
Essentially the way that you are doing it simply will not work correctly.
Why can't you select the address and city at the same time for both queries,
order them as required and them move them into a PHP array and finish any
sorting within there? That way if you are trying to say filter for
duplicates it is a lot easier to process.
On another note, it is also a good idea to have a unique numerical
auto-increment field in things like address lists, (it stops weird things
happening when two neighbours sign up to whatever service you are
offering!). Once you have an id field you can then do interesting things
like reversion queries. e.g "select id,myfield1,myfield2 where id not
in($ids_from previous_select)" wheich are helpful in what I think it is you
are doing here.


Regards,

Phil Beynon

** Infolink Electronic Systems Ltd. http://www.infolinkelectronics.co.uk
** Professional Web Design & Cobalt Hosting Solutions
** Contact: Sales at infolinkelectronics.co.uk
** Tel / Fax 0121 458 4894 (office) 0121 441 3558 (home) 07801 548464
(mobile)





More information about the Phpwm mailing list