Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : MySQL Functions : mysql_select_db

mysql_select_db

Select a MySQL database (PHP 4, PHP 5, PECL mysql:1.0)
bool mysql_select_db ( string database_name [, resource link_identifier] )

Example 1461. mysql_select_db() example

<?php

$link
= mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!
$link) {
   die(
'Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!
$db_selected) {
   die (
'Can\'t use foo : ' . mysql_error());
}
?>

Related Examples ( Source code ) » mysql_select_db
















Code Examples / Notes » mysql_select_db

doug

When you need to query data from multiple databases, note that mysql_select_db("db2")  doesn't prevent you from fetching more rows with result sets returned from "db1".
mysql_select_db("db1");
$res_db1=mysql_query("select * from foobar");
myqsl_select_db("db2);
$row_db1=mysql_fetch_object($res_db1);
$res_db2=mysql_query("select * from test where id='$row_db1->id'");


me

Problem with connecting to multiple databases within the same server is that every time you do:
mysql_connect(host, username, passwd);
it will reuse 'Resource id' for every connection, which means you will end with only one connection reference to avoid that do:
mysql_connect(host, username, passwd, true);
keeps all connections separate.


maarten

Previously posted comments about opening connections if the same parameters to mysql_connect() are used can be avoided by using the 'new_link' parameter to that function.
This parameter has been available since PHP 4.2.0 and allows you to open a new link even if the call uses the same parameters.


matsko

Just incase the mysql_select_db() function still won't work with multiple database connections (as has happened to me before).
$dbh1 = mysql_pconnect($host,$user,$pass);
$dbh2 = mysql_pconnect($host,$user,$pass);
You could do this...
mysql_query("USE database1",$dbh1);
mysql_query("Use database2",$dbh2);
This does the same thing as the mysql_select_db() function...
or this...
You don't even have to select the database for each connection.
mysql_query("SELECT * FROM database1.table",$dbh1);
mysql_query("SELECT * FROM database2.table",$dbh2);


james

Be carefull if you are using two databases on the same server at the same time.  By default mysql_connect returns the same connection ID for multiple calls with the same server parameters, which means if you do
<?php
 $db1 = mysql_connect(...stuff...);
 $db2 = mysql_connect(...stuff...);
 mysql_select_db('db1', $db1);
 mysql_select_db('db2', $db2);
?>
then $db1 will actually have selected the database 'db2', because the second call to mysql_connect just returned the already opened connection ID !
You have two options here, eiher you have to call mysql_select_db before each query you do, or if you're using php4.2+ there is a parameter to mysql_connect to force the creation of a new link.


buzz

As has been already commented, opening multiple connection handles with:
<?php
$connection_handle =  mysql_connect($hostname_and_port,$user,$password);
?>
causes the connection ID/handle to be REUSED if the exact same parameters are passed in to it.   This can be annoying if you want to work with multiple databases on the same server, but don't want to (a) use the database.table syntax in all your queries or (b) call the mysql_select_db($database) before every SQL query just to be sure which database you are working with.    
My solution is to create a handle for each database with mysql_connect (using ever so slightly different connection properties), and assign each of them to their own database permanently.  each time I do a mysql_query(...) call, I just include the connection handle that I want to do this call on eg (ive left out all error checking for simplicity sake):
<?php
// none of thesehandles are re-used as the connection parameters are different on them all, despite connecting to the same server (assuming 'myuser' and 'otheruser' have the same privileges/accesses in mysql)
$handle_db1 = mysql_connect("localhost","myuser","apasswd");
$handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd");
$handle_db3 = mysql_connect("localhost:3306","myuser","apasswd");
$handle_db4 = mysql_connect("localhost","otheruser","apasswd");
// give each handle it's own database to work with, permanently.
mysql_select_db("db1",$handle_db1);
mysql_select_db("db2",$handle_db2);
mysql_select_db("db3",$handle_db3);
mysql_select_db("db4",$handle_db4);
//do a query from db1:
$query = "select * from test"; $which = $handle_db1;
mysql_query($query,$which);
//do a query from db2 :
$query = "select * from test"; $which = $handle_db2;
mysql_query($query,$which);
//etc
?>
Note that we didn't do a mysql_select_db between queries , and we didn't use the database name in the query either.
Of course, it has the overhead of setting up an extra connection.... but you may find this is preferable in some cases...


dan ross

Another way to select from 2 different databases on the same server:
mysql_select_db("db1");
$res_db1 = mysql_query("select * from db1.foobar");
$res_db2 = mysql_query("select * from db2.foobar");
I.e. just prepend database name.


Change Language


Follow Navioo On Twitter
mysql_affected_rows
mysql_change_user
mysql_client_encoding
mysql_close
mysql_connect
mysql_create_db
mysql_data_seek
mysql_db_name
mysql_db_query
mysql_drop_db
mysql_errno
mysql_error
mysql_escape_string
mysql_fetch_array
mysql_fetch_assoc
mysql_fetch_field
mysql_fetch_lengths
mysql_fetch_object
mysql_fetch_row
mysql_field_flags
mysql_field_len
mysql_field_name
mysql_field_seek
mysql_field_table
mysql_field_type
mysql_free_result
mysql_get_client_info
mysql_get_host_info
mysql_get_proto_info
mysql_get_server_info
mysql_info
mysql_insert_id
mysql_list_dbs
mysql_list_fields
mysql_list_processes
mysql_list_tables
mysql_num_fields
mysql_num_rows
mysql_pconnect
mysql_ping
mysql_query
mysql_real_escape_string
mysql_result
mysql_select_db
mysql_set_charset
mysql_stat
mysql_tablename
mysql_thread_id
mysql_unbuffered_query
eXTReMe Tracker