我同时遇到来自同一连接的2个选择数据库的问题,代码是:
@$dbmssSQLGestasa_conn = mssql_connect($servidor, $usuario, $contra);
if(!$dbmssSQLGestasa_conn){
header("Location:nobase.php");
exit();
}
mssql_select_db('GESTASA', $dbmssSQLGestasa_conn);
其他连接的代码:
@$dbmssSQLTasa_conn = mssql_connect($servidor, $usuario, $contra);
if(!$dbmssSQLTasa_conn){
header("Location:nobase.php");
exit();
}
//Apertura de la base de datos
mssql_select_db('TASA', $dbmssSQLTasa_conn);
它不起作用并给我错误“(严重性16)”。
是否可以在同一连接mssql中进行2个或更多选择数据库?
答案 0 :(得分:0)
不要使用mysql_select_db();
在Mysqli:
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM test123.posts"; // databse.tablename
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$mack[] = $row;
}
}
$sql1 = "SELECT * FROM portal.abouts"; // databse.tablename
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
$mack1[] = $row1;
}
}
echo "<pre>";
print_r($mack);
print_r($mack1);
echo "</pre>";
$conn->close();
答案 1 :(得分:0)
您可以使用“下方代码”轻松地同时使用2个数据库 我使用变量作为大写词但你可以使用自己的变量词。
<?php
define('HOST', "YOURHOSTNAME");
define('USER', "YOURHOSTNAME");
define('PASS', "YOURHOSTNAME");
define('DATABASE1', "NAMEOFDATABASE1");
define('DATABASE2', "NAMEOFDATABASE2");
$DATABASE1 = mysqli_connect(HOST, USER, PASS, DATABASE1);
$DATABASE2 = mysqli_connect(HOST, USER, PASS, DATABASE2);
if(!$DATABASE1){
die("DATABASE1 CONNECTION ERROR: ".mysqli_connect_error());
}
if(!$DATABASE2){
die("DATABASE2 CONNECTION ERROR: ".mysqli_connect_error());
}
$sql = "SELECT * FROM TABLE"; /* You can use your own query */
$DATABASE1_QUERY = mysqli_query($DATABASE1, $sql);
$DATABASE2_QUERY = mysqli_query($DATABASE2, $sql);
$DATABASE1_RESULT = mysqli_fetch_assoc($DATABASE1_QUERY);
$DATABASE2_RESULT = mysqli_fetch_assoc($DATABASE2_QUERY);
/* SHOW YOUR RESULT HERE WHICH DATABASE YOU WANT FROM */
echo $DATABASE1_RESULT['id'];
echo $DATABASE2_RESULT['id'];
/*After complete your all work don't forgot about close database connections*/
mysqli_close($DATABASE1);
mysqli_close($DATABASE2);
?>