Jump to content

Multiple database connections


ksmatthews

Recommended Posts

HI Gurus,

 

I have a simple update form that creates a MySQL connection with a DB to display on the form all previous user preferences.

 

I also need to dynamically populate various drop down lists (select objects) by running entirely different SQL commands based upon data in different tables in the same DB. I would need to set up more DB connections in order to do this.

 

Q1.  Can I use the same connection ID ? ie can I avoid re-connecting to the same DB ?

 

Q2.  Will not multiple connections like this degrade performance ?

 

Q3.  Is there an easy way of doing this ? (running multiple queries)

 

regards,

 

Steven M

Link to comment
Share on other sites

...data in different tables in the same DB...

 

if all the data is in one physical database, you only need to make one connection.

 

<?php

mysql_connect("localhost", $username, $password);
mysql_db_connect("database_name");

//make multiple quiers now with the same connection
$query = mysql_query("SELECT * FROM table WHERE column_x = 'foo_bar' ");
$result = mysql_fetch_array($query);
print_r($result);

$query = mysql_query("SELECT * FROM table WHERE column_y = 'foo_bar' ");
$result = mysql_fetch_array($query);
print_r($result);

$query = mysql_query("SELECT * FROM table WHERE column_z = 'foo_bar' ");
$result = mysql_fetch_array($query);
print_r($result);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.