【问题标题】:fetching and comparing the data in php获取和比较php中的数据
【发布时间】:2021-06-05 09:18:14
【问题描述】:

在我的程序中,我想从不同的表中从数据库中获取数据并相互比较,但它只显示注册为字符串不相等。

<?php
$id = $_SESSION["teacher_id"];
$sql1 = "SELECT sem FROM students_data where mentor='$id'";
$sql2 = "SELECT sem FROM teacher where teacher_id='$id'";
($result1 = mysqli_query($db, $sql1)) or die(mysqli_error());
($result2 = mysqli_query($db, $sql2)) or die(mysqli_error());

if ($result1 == $result2) {
    echo "Registered";
} else {
    echo "Not Registered";
}
?>

【问题讨论】:

标签: php mysql mysqli


【解决方案1】:

您可以使用单个查询来比较两个表。该查询将选择students_data中的所有条目,如果名称与teacher中的名称不同,则is_diff为1,否则为0:

SELECT
    students_data.sem,
    IF(students_data.sem != teacher.sem, 1, 0) AS is_different
FROM
    students_data
LEFT JOIN
    teacher
ON
    teacher.teacher_id = students_data.mentor

然后你可以得到查询结果并在你的if语句中使用它:

$arr = mysqli_fetch_assoc($result)
if ($arr['is_different'] == 1) {
    echo "Registered"; } 
else { 
    echo "Not Registered";
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    相关资源
    最近更新 更多