简体   繁体   English

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

[英]how to compare the value in the same table in a different column

so i have a verification table consists of key_id, key_verification, confirm_key, and key_status 所以我有一个验证表,由key_id,key_verification,confirm_key和key_status组成

this is the sql process for client_verifycodez.php where i want to compare the value of key_verification and confirm_key if they are the same 这是client_verifycodez.php的sql进程,在这里我要比较key_verification和Confirm_key的值(如果它们相同)

the update sql is needed so that when client enter the key fro a form, it will enter into db as confirm_key. 需要更新sql,以便当客户端从表单输入密钥时,它将作为Confirm_key输入db。 so by that i want to compare the value of confirm_key with the already existing key_verification 所以我想将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");
    }

so what i need is how do i compare so that when key_verification and confirm_key is equals, it will go to home.php or else alert. 所以我需要如何比较,以便当key_verification和Confirm_key相等时,它将转到home.php或发出警报。 i think i have the problem with the sql. 我认为我有SQL问题。

can anyone perhaps help me? 有人可以帮我吗? thank you 谢谢

You can't do like this, use sub-query, 您不能这样做,请使用子查询,

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

Note: Please, don't use mysql_* functions in new code . 注意: 请不要在新代码中使用mysql_*函数 They are no longer maintained and are officially deprecated . 它们不再维护,已正式弃用 See the red box ? 看到红色框了吗? Learn about prepared statements instead, and use PDO , or MySQLi - this article will help you decide which. 而是了解准备好的语句 ,并使用PDOMySQLi- 本文将帮助您确定哪一个。 If you choose PDO, here is a good tutorial . 如果您选择PDO, 这是一个很好的教程

The mysql_num_rows() function will return the number of existing rows count mysql_num_rows()函数将返回现有行数

please use mysqli extension always 请始终使用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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将值与列名相同的多个表中的列值进行比较? - How to compare value with column values in multiple table whose column names are same? PHP - 从同一行的不同列中获取和比较行值 - PHP - Get and compare row value from a different column for the same row 如何在一个表中比较两个具有相同值但效果不同的字段? - how to compare two field with same value with different effect in one table in mysql? 选择表中具有相同表中不同列的值的条件的行 - Select rows in table with condition of value of different column in same table 如何比较2个数组与相同的键但不同的值PHP - How to compare 2 array with same key but different value PHP 如何在一行中获取具有相同 id 但在另一列中具有不同值的数据并在 php 中的表中显示一个月 - How to fetch data with same id in a row but different value in another column and display one month into table in php 如何获得具有不同列值和相同外键的记录表格? - How to get record form table has different column value and same foreign key? Laravel从同一个表中选择3行,并为指定列指定不同的值 - Laravel select 3 rows from the same table with different value for a specified column 比较不同表中的同一列 Laravel - Compare same column in different tables Laravel 如何在同一个表中显示具有相同列不同 id 的 2 个值 - How to show 2 value with same column different id in same tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM