2016-08-22 144 views
0

程序是發現從相同的服務器具有相同的表2db的差異,並顯示使用php的表中的差異。在這裏,我發現難以選擇dbname,我試圖通過表單方法獲取dbname。發現在不同的2 MySQL數據庫從相同的服務器具有相同的表使用php

<html> 
<head> 
<title>Connecting db</title> 
<style> 
table, th, td { 
    border: 1px solid black; 
} 
</style> 
</head> 
<body> 
<form method="post" action="" > 
    <label>db1 :</label> 
    <input type="text" name="db1 name"> 
    <input type="submit" name="submit" value="submit"> 
    <br><br> 
    <label>db2 :</label> 
    <input type="text" name="db2 name"> 
    <input type="submit" name="submit" value="submit"> 
</form> 
<?php 
if(isset($_POST["submit"])){$servername = "servername"; 
$username = "username"; 
$password = "password"; 
$dbname = "db"; 

$db1 = $_POST["db1 name"]; 
$db2 = $_POST["db2 name"]; 

// Create connection 
$conn = new mysqli($servername, $username, $password,$dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT application_id,application_name\n" 
    . "FROM "($db1)".applications\n" 
    . "WHERE(\n" 
    . " application_id NOT IN \n" 
    . " (SELECT application_id FROM "($db2)".applications)\n" 
    . ")\n" 
    . "GROUP BY application_id LIMIT 0, 30 "; 
$result = $conn->query($sql); 

    if ($result->num_rows > 0) { 
    echo "<table><tr><th>APPLICATION ID</th><th>APPLICATION Name</th></tr>"; 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo "<tr><td>".$row["application_id"]."</td><td>".$row["application_name"].."</td></tr>"; 
    } 
    echo "</table>"; 
} else { 
    echo "0 results"; 
} 
$conn->close(); 
} 
?> 
</body> 
</html> 

回答

1

嘗試這種連接

$conn = mysqli_connect("hostname","username","password"); 
mysqli_select_db("db1",$conn); 
mysqli_select_db("db2",$conn); 
相關問題