-1

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. It does not die. It says that it exists only because 244 exist. 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]

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 3
    mysql_* functions are deprecated as of PHP 5.5.0, and removed as of PHP 7.0.0. Switch your code to use [PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php) or [mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. Be sure to use prepared statements and parameter binding, so **you'll never have to worry about quoting issues again.** – aynber Jul 19 '22 at 19:08
  • Do you have any idea what I should change to make it work above, maybe switch to mysqli in the future. – Wille Amin Jul 19 '22 at 19:10
  • Well, first, you're going to have multiple rows, not just one, so you need to loop through the results. I would have the values you're looking for in an array, then as you go through your loop, you can [remove the value from the array](https://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key). Then when all of the results have been fetched, the array will only have the missing ids. – aynber Jul 19 '22 at 19:10
  • 73 / 5 000 Översättningsresultat It works when I use 'id' = '244' but not 'id ' IN (multiple rows) – Wille Amin Jul 19 '22 at 19:13
  • It looks like you intended this question to contain an image. However, the image URL was not included. – halfer Jul 19 '22 at 21:49

1 Answers1

2

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. This logic is assuming id is your primary key. In mysql_*, there's a mysql_num_rows function you can use.

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. If you move over to an ORM, library, or framework etc, you'll most likely move over to 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.

Please see the mysqli equivalent:
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. 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.

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.

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

Kevin Y
  • 646
  • 5
  • 18