PHP Access 2数据库[关闭]

Currently for our login system etc we use a connect.php file with the following contents

$connect_error = 'Sorry, we\'re currently experiencing connection problems.';
mysql_connect('localhost', 'x', 'x') or die($connect_error);
mysql_select_db('x');

We are currently developing a system that we would like users to be able to add devices to their account

The main table we have stores all the users info e.g. First Name, Last Name, Email, Password

To this table we are adding the following fields - Device 1, Device 2, Device 3

Along side this we would also like to have access to another database, we want to hold the device id's in the main database so we know who owns what devices.

The second database will consist of Device ID, Lat, Long, Time

We would like to be able to read out of either database, I'm not sure how we would specify the database to connect to as I've only ever dealt with the one database

I'm not sure if its doable or if i'm going about it the totally wrong way - Both databases will be on the same server and we can assign the same user to both databases if that helps

Thanks

First of all, You should not use mysql_* functions as the are deprecated. Use MySQLi or PDO for this.

In Mysql the solution is to use the link identifier returned by mysql_connect. But if you use MySQLi or PDO you need to create those object first and use that object. OOP interface of MySQLi has NO concept of link identifier. Read this page from PHP manual that helps you to migrate. For PDO use this tutorial to learn.

For MySQLi extension, its already a object and the object contains database information.

$db1 = new mysqli(...);
$db2 = new mysqli(...);

// sending query in database 1
$db1->query("...") 

// sending query in database 2
$db2->query("...") 

For MySQL extension, mysql_connect() returns a links identifier. You should save it in a variable and use it.

$link1 = mysql_connect();
mysql_select_db($dbnane, $link1);

$link2 = mysql_connect();
mysql_select_db($dbnane, $link2);

In query you should use it like this,

mysql_query($query, $link1); // query in first db

mysql_query($query2, $link2); // query in second db;

Shouldn't use as mysql_* functions are deprecated:

$database_handler_1 = mysql_connect("localhost", "username", "password");
// The fourth parameter is for "new link" 
$database_handler_2 = mysql_connect("localhost", "username", "password", true); 

// Insert as parameter the database handler
mysql_select_db('database1', $database_handler_1);
mysql_select_db('database2', $database_handler_2);

// And run queries with the database handler as second parameter:
mysql_query("SELECT * FROM table", $database_handler_1);
mysql_query("SELECT * FROM table", $database_handler_2);

Can use instead:

$database_handler_1 = new PDO('mysql:host=localhost;dbname=database1', $user, $pass);
$database_handler_2 = new PDO('mysql:host=localhost;dbname=database2', $user, $pass);
$database_handler_1->query('SELECT * from FOO')
$database_handler_1->query('SELECT * from BAR')

(Look @ the full PDO documentation here: http://php.net/manual/en/pdo.connections.php)