We Talk Coding

Join databases? PHP help?

Okay I have a database of people that work in certain cities. I have a second database with other (secondary) cities (near by) that they work in. What I want to do is go into the first database and see if there is no one listed for that city. If so, go into the other database and find those who have that city listed as a secondary city. Then get their ID and go back into the first database and get their info (using ID). Code: check for primaries if($cat_rows == 0) { $cat_altresult = DatabaseSelect(MY FUNCTION.. ITS FINE searches for secondary cities get array??); $cat_rows = sizeof($cat_altresult); if($cat_rows != 0) { $alt_msg = "blah"; } else $alt_msg = "blah blah"; } Take IDs from secondary cities, go into primary table and gather info for EVERYONE who has that city listed as a secondary. Getting the multiple rows is my problem... i think... someone recommend a join, but I am not sure how to do this on two databases. Thanks Someone recommend a join, but Im n I meant tables... :\ same DB two tables... my bad

Public Comments

  1. I don't think you need to do a join. Are you using a MySQL db or something else? I recommend, whatever you use, to use PDO as an abstraction layer to your database. Using PDO you can get a result set from your query and then loop through each result with a for each statement. More info on setting up and using PDO can be found http://us3.php.net/manual/en/ref.pdo.php If PDO is not an option, most DB functions allow you to loop through a result set using a while loop like: while ($rs = fetchRow($resultSet)){ //do stuff with the data }
  2. It works best if you are using two tables in one database, not two independant databases. A join is just a select statement using different tables. Say you had a table xxx and a table yyy. xxx is about your customers and yyy has city info. Assuming your xxx file has a city id for each entry that correspoonds to a city_id field in yyy: SELECT * FROM xxx, yyy WHERE xxx.city_id = yyy.city_id
Powered by Yahoo! Answers