mysql_select_db

(PHP 4, PHP 5)

mysql_select_dbWählt eine MySQL-Datenbank aus

Warnung

Diese Erweiterung ist seit PHP 5.5.0 als veraltet markiert und wurde in PHP 7.0.0 entfernt. Verwenden Sie stattdessen die Erweiterungen MySQLi oder PDO_MySQL. Weitere Informationen bietet der Ratgeber MySQL: Auswahl einer API. Alternativen zu dieser Funktion umfassen:

Beschreibung

mysql_select_db(string $database_name, resource $link_identifier = NULL): bool

Setzt die aktive Datenbank auf dem Server, die mit der angegebenen Verbindung verknüpft ist. Jeder nachfolgende Aufruf von mysql_query() operiert auf dieser aktiven Datenbank.

Parameter-Liste

database_name

Der Name der auszuwählenden Datenbank.

link_identifier

Die MySQL-Verbindung. Wird die Verbindungskennung nicht angegeben, wird die letzte durch mysql_connect() geöffnete Verbindung angenommen. Falls keine solche Verbindung gefunden wird, wird versucht, eine Verbindung aufzubauen, wie es beim Aufruf von mysql_connect() ohne Angabe von Argumenten der Fall wäre. Falls zufällig keine Verbindung gefunden oder aufgebaut werden kann, wird eine Warnung der Stufe E_WARNING erzeugt.

Rückgabewerte

Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben.

Beispiele

Beispiel #1 mysql_select_db()-Beispiel

<?php

$link
= mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!
$link) {
die(
'Verbindung nicht möglich : ' . mysql_error());
}

// Verwende Datenbank foo
$db_selected = mysql_select_db('foo', $link);
if (!
$db_selected) {
die (
'Kann foo nicht verwenden : ' . mysql_error());
}
?>

Anmerkungen

Hinweis:

Für die Abwärtskompatibiliät kann der folgende veraltete Alias verwendet werden: mysql_selectdb()

Siehe auch

add a note add a note

User Contributed Notes 8 notes

up
15
james at gogo dot co dot nz
20 years ago
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.
up
1
Maarten
18 years ago
About opening connections if the same parameters to mysql_connect() are used: this 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.
up
-24
Anonymous
10 years ago
function go_mysql($query)
{
    global $mysql_link;
   
    if (!$mysql_link)
    {
        $mysql_link = mysql_connect("localhost","root","my_pass") or die(mysql_error());
        mysql_select_db("my_db") or die(mysql_error());
        mysql_query("SET NAMES 'utf8'");
        mysql_query("set character_set_client='utf8'");
        mysql_query("set character_set_results='utf8'");
        mysql_query("set collation_connection='utf8'");
        global $mysql_link;
    }
       
    $result=mysql_query($query);
    if ($result)
    {
        return $result;
    }
    else
    {
        echo "Database Error: " . mysql_error()."<br><b>$query</b>";
        die();
    }
}
up
-23
anotheruser at example dot com
15 years ago
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);

?>
up
-30
miloshio at gmail dot com
11 years ago
You can select MySQL database without using this function.
Simply right after connecting to MySQL
<?php $connection = mysql_connect("dabatbasehost", "username", "password"); ?>
perform this query:
<?php mysql_query("USE somedatabase", $connection); ?>
up
-30
duncan at berrimans dot co dot uk
11 years ago
Note that the manual is slightly misleading it states :-

"Sets the current active database on the server that's associated with the specified link identifier. Every subsequent call to mysql_query() will be made on the active database."

The 2nd statement is not true or at best unclear.

mysql_query() manual entry actually correctly states it will use the last link opened by mysql_connect() by default.

Thus if you have 2 connections you will need to specify the connection when calling mysql_query or issue the connect again to ensure the 1st database becomes the default, simply using mysql_select_db will not make the 1st database the default for subsequent calls to mysql_query.

Its probably only apparent when the two databases are on different servers.
up
-34
me at khurshid dot com
16 years ago
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.
up
-57
riad93 at mail dot ru
14 years ago
You can use DataBases without <?php mysql_select_db() ?>

And you will havenot james at gogo dot co dot nz's problems :)

<?php
mysql_connect
('localhost','db_user','pssword');
mysql_query('SELECT * FROM database_name.table_name');

?>
To Top