如何比较不同列中同一个表中的值

时间:2014-01-17 06:55:31

标签: php mysql sql comparison

所以我有一个验证表,包括key_id,key_verification,confirm_key和key_status

这是client_verifycodez.php的sql进程,我想比较key_verification和confirm_key的值,如果它们是相同的

需要更新sql,以便当客户端从表单输入密钥时,它将以confirm_key的形式进入db。所以我希望将confirm_key的值与现有的key_verification

进行比较
    $sql1 = "UPDATE verification SET confirm_key = '".$confirm_key."' WHERE key_id ='".$id."'";
    mysql_query($sql1);

    $sql = ("SELECT * FROM verification WHERE key_verification = confirm_key");
    $query = mysql_query($sql) or die ("Error: " . mysql_error());
    $check = mysql_fetch_array($query);

    if($check==true)
    {
    echo "<center>";
    echo "Your key is invalid!";
    echo "<br>";
    echo "<a href=client_verifycodez.php>Back </a>";
    echo "</center>";
    }
    else
    {
    header("Location: home.php");
    }

所以我需要的是如何比较,以便当key_verification和confirm_key相等时,它将转到home.php或者提醒。我想我有sql的问题。

任何人都可以帮助我吗?谢谢

2 个答案:

答案 0 :(得分:1)

你不能这样做,使用子查询,

"SELECT * FROM verification WHERE key_id =". $id ." AND key_verification
 = (SELECT confirm_key FROM verification WHERE key_id =". $id .")"

注意: Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分:0)

mysql_num_rows()函数将返回现有行数

请始终使用mysqli扩展

<?php

$sql = ("SELECT * FROM verification WHERE key_verification = confirm_key");

$query = mysql_query($sql) or die ("Error: " . mysql_error());

//get the number of result rows
$num_rows = mysql_num_rows($query);

//get the details
$check = mysql_fetch_array($query);

if($query)
{
    if($num_rows>0)
    {
    echo "<center>";
    echo "Your key is invalid!";
    echo "<br>";
    echo "<a href=client_verifycodez.php>Back </a>";
    echo "</center>";
    }
    else
    {
    header("Location: home.php");
    }
}