php - While in while 只显示一行?

标签 php pdo while-loop

我有两个 SQL 表,一个包含“手动插入的数据”,另一个包含“通过脚本自动插入的数据”。

为了测试脚本是否正常运行,手动表和自动表是一样的。

所以,我想“比较”两个数据库,然后在另一个脚本中突出显示差异。

// $currentdate_today_midnight is a timestamp

$sql_getLive = "SELECT * FROM worksheets WHERE entry_date > $currentdate_today_midnight";
$req_getLive = $cnx->query($sql_getLive);
$req_getLive->setFetchMode(PDO::FETCH_OBJ);

// countAll counts all rows for a table and a condition 
$countLive = countAll("worksheets", "entry_date > $currentdate_today_midnight");

$sql_getCriter = "SELECT * FROM criter_live WHERE entry_date > $currentdate_today_midnight";
$req_getCriter = $cnx->query($sql_getCriter);
$req_getCriter->setFetchMode(PDO::FETCH_OBJ);

$countCriter = countAll("criter_live", "entry_date > $currentdate_today_midnight");

if($countLive == 0){
    /* If there is no live (manual datas) inserted */
    echo "EMPTY";
    die();
}

while ($check_criter = $req_getCriter->fetch()) {

    while ($check_live = $req_getLive->fetch()) {

        if ($check_live->train_id == $check_criter->train_id) {

            /* check_live = worksheets */
            /* check_criter = criter_live */

            echo $check_live->train_id . "|" . $check_criter->entry_date . "|" . $check_live->entry_date . "|". $check_criter->left_date . "|". $check_live->entry_date . "|". $check_criter->train_type . "|". $check_live->train_type . "|". $check_criter->entry_number . "|". $check_live->entry_number . "|". $check_criter->left_number . "|". $check_live->left_number. "#";

        }
    }
}

因此,我尝试制作“一会儿一会儿”,但它不起作用,我只得到一个“echo ”……而不是 17 个(由于 countAll 函数返回)。

我做错了吗?或者还有其他解决办法吗?

谢谢!

最佳答案

也许你可以尝试像这样直接在 sql 中找到差异:

select * from `worksheets` where `entry_date` > $currentdate_today_midnight
    and `train_id` not in (
        select `train_id` from `criter_live` where `entry_date` > $currentdate_today_midnight
    )

稍作修改的版本,测试entry_date

select * from `worksheets` where `entry_date` > $currentdate_today_midnight
    and `entry_date` not in (
      select `entry_date` from `criter_live` where `entry_date` > $currentdate_today_midnight
    )

关于php - While in while 只显示一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35172312/

相关文章:

java - While 循环不更新 TreeMap 的值

php - 链接页面的数学方程式

php - 为什么需要设计 "404 not found"页?

php - 使用 php 中的函数创建数据库连接?

php - 如何获取 PDO 错误中的列名?

在不使用 c99 标准的情况下使用 bool 编译 c 代码

php - Symfony 4 Doctrine无法连接到Docker MySQL

php - 我希望它有一个下拉列表,其中的选择来自 mysql 表,而不是在文本框中输入

php - fatal error 异常: Error: Class 'PDO' not found on symfony2

javascript - 从 PHP 获取 Javascript 中的 .src