hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Selecting Two Databases At Same Time In Loop
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Selecting Two Databases At Same Time In Loop

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 01-23-2011, 10:30 PM
kgiger kgiger is offline
Temporarily Suspended
 
Join Date: Jan 2011
Posts: 51

Selecting Two Databases At Same Time In Loop


What I am trying to do is grab a users city and state from one database and then grab the zip code from another database and then update the first databases record so that zip code is in that db also.

here is what I got but I can figure out how to make the second command to connect to second db.

PHP Code:
 $result mysql_query("SELECT * FROM profiles"
 or die(
mysql_error());  


 while(
$row mysql_fetch_array$result )) {


    
$xid $row['id'];
    
$xcity $row['city'];
    
$xstate $row['state'];
 



Reply With Quote


Sponsored Links
  #2  
Old 01-23-2011, 11:52 PM
webexperts webexperts is offline
Newbie
 
Join Date: Jan 2011
Posts: 18
You can select two databases in a loop ,
but u have to use link identifier, that will be returned when connecting to a database ....

Reply With Quote
  #3  
Old 01-24-2011, 11:31 AM
Christian Little Christian Little is offline
Web Hosting Guru
 
Join Date: Jan 2011
Location: Vancouver, Canada
Posts: 329
Here's what you need to do. This code won't work obviously, but it's the rough idea of how to make it work.

Basically when you have multiple DB connections established you just pass in an optional mysql link resource to the mysql_query command, which tells php which database connection to run that specific query on:

PHP Code:
// connect to the 1st database that you are trying to update
$linkCities mysql_connect(blah blah blah);
mysql_select_db("db_name"$linkCities) || die ("Could not select database: " mysql_error());

// connect to the 2nd database that has the zipcodes
$linkZips mysql_connect(blah blah blah);
mysql_select_db("db_name"$linkZips) || die ("Could not select database: " mysql_error());

$city "Seattle";
$state "WA";


// Pull the Zip code from the 2nd database
$query "SELECT * FROM table_name WHERE state=\"$state\" and city=\"$city\"";
$result mysql_query($query$linkZips);
while(
$row mysql_fetch_assoc($result)) {

  
// Update the 1st database with the zipcode
  
$update_query "UPDATE table_name SET zip = \"" $row["zip"] . \"" WHERE city = \"$city\" and state = \"$state\"";
  
$update_result mysql_query($update_query$linkCities);
}

mysql_close($linkCities);
mysql_close($linkZips); 
You can see more examples and the details of the mysql_query function here: http://php.net/manual/en/function.mysql-query.php


Last edited by Christian Little; 01-24-2011 at 11:39 AM.
Reply With Quote
Sponsored Links
  #4  
Old 01-24-2011, 01:28 PM
kgiger kgiger is offline
Temporarily Suspended
 
Join Date: Jan 2011
Posts: 51
Ok thanks that worked great!!

Reply With Quote
  #5  
Old 01-25-2011, 12:32 AM
InspiRunner InspiRunner is offline
Newbie
 
Join Date: Nov 2007
Posts: 15
Just for fun - MySQL allows you to execute queries that pull data from two databases at once. This would make sense if tables can be joined.

PHP Code:
  $mysqlusr "root";
  
$mysqlpass "";
  
$dbname "database1";
  
$idlink mysql_connect('localhost'$mysqlusr$mysqlpass)
  or die(
"Could not connect: " mysql_error());
  
mysql_select_db($dbname);

  
$qry "select * from database1.table1 a inner join database2.table2 b on a.id=b.id";
  
$res mysql_query($qry);

  while(
$rs mysql_fetch_array($res))
  {
    
// run update query here
  


__________________
PHPRunner - building the best PHP code generator, one byte at the time
Forms, AJAX, reports, charts, advanced security, Google maps, CAPTCHA and more

Reply With Quote
  #6  
Old 01-28-2011, 04:14 PM
synrg synrg is offline
Newbie
 
Join Date: Jan 2011
Location: Arizona
Posts: 11
Quote:
Originally Posted by InspiRunner View Post
Just for fun - MySQL allows you to execute queries that pull data from two databases at once. This would make sense if tables can be joined.

PHP Code:
  $mysqlusr "root";
  
$mysqlpass "";
  
$dbname "database1";
  
$idlink mysql_connect('localhost'$mysqlusr$mysqlpass)
  or die(
"Could not connect: " mysql_error());
  
mysql_select_db($dbname);

  
$qry "select * from database1.table1 a inner join database2.table2 b on a.id=b.id";
  
$res mysql_query($qry);

  while(
$rs mysql_fetch_array($res))
  {
    
// run update query here
  

That would have been my solution also.

Reply With Quote
Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting from database, limit by time P-nut Programming Discussion 9 10-01-2007 07:33 PM
Running a loop inside of another loop? i.make.sense Programming Discussion 12 05-02-2006 02:28 PM
What is a loop and when do I need one? Dave18 Colocation and Data Centers 10 03-24-2005 02:14 AM
Helpdesk at 8-95.com - Response time when selecting "emergency" priority? mrzippy Web Hosting 12 08-09-2004 10:48 PM
else in while loop code-2k3 Programming Discussion 15 12-16-2003 10:52 AM

Related posts from TheWhir.com
Title Type Date Posted
Cloud Platform Heroku Offers Free Ruby on Rails Training for Developers Web Hosting News 2013-04-12 14:41:30
Rackspace Offers New Cloud Database Service Free Until September Web Hosting News 2012-08-09 14:33:06
VMware vFabric Data Director 2.0 Adds Support for Oracle Databases Web Hosting News 2012-07-10 16:30:32
Web Host Rackspace Launches Private Beta for MySQL Cloud Database Web Hosting News 2011-12-01 21:09:51
Microsoft to Offer New Database Consolidation Appliance, Enhances Cloud Services Web Hosting News 2011-10-14 20:40:47


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?