如何在PHP中检查两个数据库是否有相同的查询并显示结果?

Irf*_*lam 1 php mysql sql database

请阅读我之前的问题以获得清晰的理解.

由于form.php是将数据保存到两个不同的数据库表中.在两个数据库中SNo都是唯一列并自动递增.每次SNo都会增加,数据会保存在该行中.对于单个数据,SNo在数据库表中保持相同.

我在数据库中添加了一些列.现在数据库看起来像

SNo   |  fname   |  lname  |    mobile    |    Request    |   status  |

1     |  John    |  Alto   |  9999999999  |   Send M1     |  Not Sent |   
2     |  khan    |  asif   |  8888888888  |   Send D1     |  Not Sent |
3     |  John    |  Alto   |  9999999999  |   Send M2     |  Not Sent |   
4     |  khan    |  asif   |  8888888888  |   Send D2     |  Not Sent |
5     |  John    |  Alto   |  9999999999  |   Send M3     |  Not Sent |   
6     |  khan    |  asif   |  8888888888  |   Send D3     |  Not Sent |
7     |  Kerr    |  Lync   |  7878787878  |   Send F3     |  Not Sent |   
8     |  Sara    |  goda   |  8585858585  |   Send L2     |  Not Sent |
Run Code Online (Sandbox Code Playgroud)

由于form.php是将数据从表单保存到两个不同的数据库.数据库表上的数据完全相同.让数据库上的表名是mytable

我创建了一个html表单来检查状态,check.html

<form action="status.php" method="get">
Enter SNo : <input type="text" name="SNo"><br>
<input type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)

和status.php

<?php
$servername = "localhost";
$username = "db";
$password = "123123";
$dbname = "db";

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

$sql = "SELECT * from mytable
    WHERE SNo = '$SNo'";



$result = $conn->query($sql);
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
    if (empty($columns)) {
        $columns = array_keys($row);
    }
    $resultset[] = $row;
}

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
echo "<thead><tr><th>SNo</th><th>First Name</th><th>USN</th><th>Last Name</th><th>Mobile</th></tr></thead><tbody><tr><td>{$row['SNo']}</td><td>{$row['fname']}</td><td>{$row['lname']}</td><td>{$row['mobile']}</td><td>{$row['Request']}</td></tr></tbody>";
echo " Delivered or Not Delivered" // What should be code here ?

// Print the data
while($row = mysql_fetch_row($result)) {
    foreach($row as $_column) {
        echo "{$_column}";
    }
}
    }
} else {
    echo "SNo Not Found";

}
$conn->close();

?>
Run Code Online (Sandbox Code Playgroud)

我希望SNo首先签入db1,如果它不存在则回显"未找到".如果它在那里db1它应该再次检查db2,如果它也在那里db2回声"未交付".如果它在db1但不在db2那时回声"已交付".应该在status.php代码中进行哪些更改才能执行此类操作?

Chr*_*del 5

这段代码可以使用一些优化,我会仔细检查这个响应,但这应该做你想要的.我删除了似乎很少有多余的循环.

<?php
$servername = "localhost";
$username = "db";
$password = "123123";
$db1Name = "db";

$servername2 = "localhost";
$username2 = "db2";
$password2 = "1231232";
$db2Name = "db2";

// Create connection
$conn = new mysqli($servername, $username, $password, $db1Name);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$SNo = mysqli_real_escape_string($conn, $_GET['SNo']);

$sql = "SELECT * from mytable
    WHERE SNo = '$SNo'";

$db1Result = $conn->query($sql);

if ($db1Result->num_rows > 0) {     // If data found in db1 ...

    // Echo the data from db1    
    while($row = $db1Result->fetch_assoc()) {
        echo "<thead><tr><th>SNo</th><th>First Name</th><th>USN</th><th>Last Name</th><th>Mobile</th></tr></thead><tbody><tr><td>{$row['SNo']}</td><td>{$row['fname']}</td><td>{$row['lname']}</td><td>{$row['mobile']}</td><td>{$row['Request']}</td></tr></tbody>";


        // Now, check if it's present in db2
        $conn2 = new mysqli($servername2, $username2, $password2, $db2Name);
        $db2Result = $conn2->query($sql);

        if ($db2Result->num_rows > 0) { // If data is found in db2...
            echo "Not delivered";
        } else { // If data is NOT in db2...
            echo "Delivered";
        }
    }
} else { // If data is NOT found in db1...
    echo "SNo Not Found";

}
$conn->close();

?>
Run Code Online (Sandbox Code Playgroud)

当你处理这样令人困惑的逻辑时,经常评论这是很好的做法 - 它将有助于保持你的理智.:)不要*在你的SELECT陈述中使用它也是一个好主意; 相反,只指定所需列的名称.

作为一个更一般的观点,我不确定你为什么试图在原始代码中循环遍历结果集三次 - 我假设你试图诊断一个问题 - 而且,同时使用mysqlimysqlAPI一起使用不会工作 ; 你应该只使用mysqli函数,并且不需要在这里多次循环MySQL结果.您尝试传递的资源mysql_fetch_row()甚至不是正确的类型,因此该调用注定要失败.