[英]How do I check if data in a column already exists and to check the second column of that row to equal a specific number
大家!
我在创建MySQL语句时遇到麻烦。
例:
我有一个名为“ keys”的列,另一列名为“ isUsed”,“ keys”列由许多不同的密钥组成,格式为:xxxx-xxxx-xxxx,我想检查该密钥是否已被使用或不。
这是表格的格式:
Key | isUsed
xxxx-xxxx-xxxx | 0
我已经做到了,所以它通过使用以下代码检查密钥是否存在:
$arrExistKey = mysqli_query($resDBCon, "SELECT Key FROM keys WHERE Key = '$strKey'");
$intKeys = mysqli_num_rows($arrExistKey);
if ($intKeys != 1) {
sendError('The beta-key does not exist!');
}
因此,我已经检查了密钥是否存在,现在我只是想弄清楚如何检查密钥是否已被使用。
如果有人可以帮助我,那就太好了! 谢谢。
一种解决方案是将您的SQL语句修改为
"SELECT Key, isUsed FROM keys WHERE Key = '$strKey'"
现在,您可以使用解决方案检查密钥是否存在,然后通过mysqli_fetch_array
函数检索isUsed
的值。
另外,在将$strKey
插入SQL语句之前,请检查$strKey
是否不包含非法字符。 根据代码的其余部分,它可能容易受到SQL注入的攻击。
祝您实施顺利!
这可能是您需要的:
// SELECT THE KEY YOU NEED
$result = mysqli_query($resDBCon, "SELECT * FROM keys WHERE Key = '$strKey'");
// LOOP ALL RESULTS AND ENTERING IN THE WHILE MEANS THAT THE KEY EXISTS
while($row = mysqli_fetch_array($result) {
// HERE YOU CHECK IF KEY WAS USED
if($row['isUsed'] == 1)
echo 'Key was used';
}
仅在SQL中编码逻辑怎么样?
$arrExistKey = mysqli_query($resDBCon, "SELECT Key FROM keys WHERE Key = '$strKey' AND isused = 1");
这就是您所需要的!
<?php
$arrExistKey = mysqli_query($resDBCon, "SELECT Key FROM keys WHERE Key = '$strKey'");
$intKeys = mysqli_num_rows($arrExistKey);
$sql = mysqli_query($resDBCon, "SELECT * FROM keys WHERE Key = '$strKey'");
while ($row = mysql_fetch_array($sql)) {
$used = $row['isUsed'];
}
if ($intKeys != 1) {
sendError('The beta-key does not exist!');
} else {
if ($used == 1) {
echo "the key has already been used recently!";
} else {
//action
}
}
?>
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.