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
https://forums.phpfreaks.com/topic/72593-multiple-database-connections/
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);

?>

Archived

This topic is now archived and is closed to further replies.

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