简体   繁体   English

如果其中一个不存在,那么它将死去。 (请帮助 MYSQL)

[英]If one of them does not exist, then it will die. (MYSQL HELP PLEASE)

I want, for example, if one of them does not exist, then it will die.例如,我想,如果其中一个不存在,那么它就会死掉。

In this case, there are 244 in the database, but 255 does not exist.在这种情况下,数据库中有 244 个,但 255 个不存在。 It does not die.它不会死。 It says that it exists only because 244 exist.它说它存在只是因为 244 存在。 But I want if one does not exist, it will die.但我想,如果一个人不存在,它就会死去。

How do I fix?我该如何解决?

$sql_result = mysql_query("SELECT `id` FROM `bokningar_bokat` WHERE `id` IN ('255','244')") or die(mysql_error());
$sql_row = mysql_fetch_assoc($sql_result);
if(!empty($sql_row['id'])) 
{
echo "Id exists - " .  $sql_row['id'] . "\n <BR/>";
} 
else
{
echo "Id no longer exists - " . $sql_row['id'] . "\n <BR/>";
die;
}

[ID 255 does not exist in the database][1] [ID 255 在数据库中不存在][1]

Probably count the resulting rows and make sure it matches the count of the in , in this case that count would be 2 as you're checking for 2 different values.可能计算结果行并确保它与in的计数相匹配,在这种情况下,当您检查 2 个不同的值时,计数将为 2。 This logic is assuming id is your primary key.此逻辑假设 id 是您的主键。 In mysql_*, there's a mysql_num_rows function you can use.在 mysql_* 中有一个可以使用的 mysql_num_rows 函数。

But please move away from the mysql_* extension function, since this extension is no longer maintained and removed as of PHP 7. An easy transition would be to the mysqli_* functions which work very similar to the mysql_* functions.但是请远离 mysql_* 扩展函数,因为从 PHP 7 开始不再维护和删除这个扩展。一个简单的转换是 mysqli_* 函数,它的工作方式与 mysql_* 函数非常相似。 If you move over to an ORM, library, or framework etc, you'll most likely move over to PDO.如果您迁移到 ORM、库或框架等,您很可能会迁移到 PDO。 One of the benefits of moving over is having support for prepared queries so you're resistant against sql injection, because user input won't be able to alter the query's structure being ran, only the input sent to it.迁移的好处之一是支持准备好的查询,因此您可以抵抗 sql 注入,因为用户输入将无法更改正在运行的查询的结构,只能更改发送给它的输入。

Please see the mysqli equivalent:请参阅 mysqli 等效项:
https://www.php.net/manual/en/mysqli-result.num-rows.php https://www.php.net/manual/en/mysqli-result.num-rows.php

You might also consider altering the query and wrapping the select into a count like so:您还可以考虑更改查询并将选择包装成一个计数,如下所示:

SELECT count(id) as 'count' FROM `bokningar_bokat` WHERE `id` IN ('255','244')

you can also use count(*) which works basically the same.您也可以使用基本相同的count(*) One of the advantages of this is that even if no matches are found, the query would still give you a result.这样做的好处之一是,即使没有找到匹配项,查询仍会为您提供结果。 Otherwise you'd need to check the number of resulting rows returned.否则,您需要检查返回的结果行数。

Finally you can get a bit fancy with for example:最后,您可以对例如以下内容有所了解:

SELECT (count(`id`) = 2) as 'foundAll' FROM `bokningar_bokat` WHERE `id` IN ('255','244')

Then foundAll will be either 1 or 0, depending upon if it found all the ids or not.然后 foundAll 将是 1 或 0,这取决于它是否找到了所有 id。

This sort of code can get messy pretty quick.这种代码很快就会变得混乱。 If your list is small, it might make sense to just pull all the matching records, and do the looping and counting logic within PHP itself.如果您的列表很小,那么只提取所有匹配的记录并在 PHP 本身中执行循环和计数逻辑可能是有意义的。

See also:也可以看看:
https://dba.stackexchange.com/questions/211355/how-to-check-if-all-elements-of-an-array-exists-in-a-table https://dba.stackexchange.com/questions/211355/how-to-check-if-all-elements-of-an-array-exists-in-a-table

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM