请阅读my previous question以便清楚地理解。
因为 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 |
由于 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>
和 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();
?>
我希望 SNo
应该首先在 db1
中检查,如果那里不存在,则回显“未找到”。如果它在 db1
中,那么它应该在 db2
中再次检查,如果它也在 db2
中,则回显“Not Delivered”。如果它在 db1
中但不在 db2
中,则回显“Delivered”。
应在 status.php 代码中进行哪些更改才能执行此类操作?
最佳答案
此代码可以使用一些优化,我会在仔细检查此响应时进行优化,但这应该可以满足您的要求。我已经删除了一些似乎无关的循环。
<?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();
?>
当您处理像这样令人困惑的逻辑时,经常发表评论是一种很好的做法,这将有助于您保持理智。 :) 最好不要在 SELECT
语句中使用 *
;相反,仅指定您需要的列的名称。
作为更一般的观点,我不确定您为什么要在原始代码中尝试遍历结果集 3 次——我假设您是在尝试诊断问题——而且,同时使用 mysqli
和 mysql
API 一起使用将不起作用;你应该只使用 mysqli
函数,这里不需要多次循环 MySQL 结果。您尝试传递给 mysql_fetch_row()
的资源甚至不是正确的类型,因此该调用注定会失败。
关于php - 如何在 PHP 中检查两个数据库的相同查询并显示结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29755142/