PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mysql_set_charset> <mysql_result
Last updated: Fri, 30 Jan 2009

view this page in

mysql_select_db

(PHP 4, PHP 5)

mysql_select_dbMySQL データベースを選択する

説明

bool mysql_select_db ( string $database_name [, resource $link_identifier ] )

指定したリンク ID が指すサーバ上のデータベースを、アクティブな データベースに設定します。それ以降にコールされる mysql_query() は、すべてアクティブなデータベース上で 実行されます。

パラメータ

database_name

選択するデータベース名。

link_identifier

MySQL 接続。 指定されない場合、mysql_connect() により直近にオープンされたリンクが 指定されたと仮定されます。そのようなリンクがない場合、引数を指定せずに mysql_connect() がコールした時と同様にリンクを確立します。 リンクが見付からない、または、確立できない場合、 E_WARNING レベルのエラーが生成されます。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例1 mysql_select_db() の例

<?php

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

// foo をカレントの db に指定する
$db_selected mysql_select_db('foo'$link);
if (!
$db_selected) {
    die (
'Can\'t use foo : ' mysql_error());
}
?>

注意

注意: 下位互換のために、次の非推奨別名を使用してもいいでしょう。 mysql_selectdb()

参考



mysql_set_charset> <mysql_result
Last updated: Fri, 30 Jan 2009
 
add a note add a note User Contributed Notes
mysql_select_db
poet at malthea dot com
02-Nov-2008 03:24
Expanding on what matsko at rogers dot com said,

If using a database user account that is the same across all databases, you only need to open a single connection and it will still let you access the multiple databases.

Also, a user might not want to open a persistent connection. I found that you don't need to, so mysql_connect is used instead of mysql_pconnect.

<?php

// A single administrative user with access to both databases
$handle = mysql_connect($host,$user,$pass);

// Then you could do this:
mysql_query("SELECT * FROM database1.table ",$handle);
mysql_query("SELECT * FROM database2.table ",$handle);

/**
OR
**/

$handle = mysql_connect($host,$user,$pass);
// using first database for the following query(queries)
mysql_query("USE database1",$handle);
mysql_query("SELECT * FROM table");

// use the second database for the following query(queries)
mysql_query("USE database2",$handle);
mysql_query("SELECT * FROM table");
?>

So you can use the same connection for both databases.. the problem with the second method (using USE to select a database) is that you can confuse which table is which. The easiest is to just prepend the database name to the table (database.table).

The benefit to using the first method may be if you have multiple applications with identical tables and fields that you want to easily switch between.
anotheruser at example dot com
13-Aug-2008 02:57
Cross-database join queries, expanding on Dan Ross's post...

Really, this is a mysql specific feature, but worth noting here.  So long as the mysql user has been given the right permissions to all databases and tables where data is pulled from or pushed to, this will work.  Though the mysql_select_db function selects one database, the mysql statement may reference another (the syntax for referencing a field in another db table being 'database.table.field').

<?php

$sql_statement
= "SELECT
    PostID,
    AuthorID,
    Users.tblUsers.Username
    FROM tblPosts
    LEFT JOIN Users.tblUsers ON AuthorID = Users.tblUsers.UserID
    GROUP BY PostID,AuthorID,Username
    "
;

$dblink = mysql_connect("somehost", "someuser", "password");
mysql_select_db("BlogPosts",$dblink);
$qry = mysql_query($sql_statement,$dblink);

?>
Martin Sarsini
21-May-2008 12:39
just to improve buzz at oska dot com solution, it is definitely the best solution so far

but it works only if you use the new_link parameter in the mysql_connect, setting it to true
be aware that the new_link parameter was introduced starting from PHP 4.2
if you don't set the new_link to true, you will need to specify the database name followed by table name in the FROM query, ie. FROM databaseName.tableName, and probably in the SELECT

more information here
http://uk.php.net/function.mysql-connect
me at khurshid dot com
09-Sep-2007 09:03
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.
matsko at rogers dot com
10-May-2006 10:19
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);
Maarten
19-Aug-2005 03:09
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.
buzz at oska dot com
06-May-2005 03:39
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
12-Feb-2004 11:43
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.
james at gogo dot co dot nz
17-Jan-2004 02:45
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.
doug at xamo dot com
17-Dec-2003 10:39
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'");

mysql_set_charset> <mysql_result
Last updated: Fri, 30 Jan 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites