0

How would I go about seeing if order id is taken and if it is taken then to make another one and see if its unique in the database.

I've got this prototype down however no luck. Doesn't execute if there is nothing in the database. Also doesn't do anything if order id is not unique.

$order_id = md5(uniqid(mt_rand(), true));

foreach($conn->query('SELECT * FROM `bucks-orders`') as $item){
    if ($item['order-id'] != $order_id){
        $insertOrder = "<sql code here>";

        $conn->query($insertOrder);
        break;
    }
}
  • If you need to check if isn't in database just use WHERE order-id=$order_id with do while loop for database rows. – pepeD May 08 '22 at 19:56
  • 1
    Why not use [RFC 4122 (UUID)](https://en.wikipedia.org/wiki/Universally_unique_identifier?msclkid=c0b859d1cf0811ecba0f2ec806c0a5ef) which has a one in a billion chance of being duplicated. [See an example](https://3v4l.org/mr0aU) Otherwise, if you're using numeric values, why not use `AUTO_INCREMENT` on your primary key? – Jaquarh May 08 '22 at 19:58
  • `$conn->query()` returns a [mysqli_result](https://www.php.net/manual/en/class.mysqli-result.php) or a [PDOStatement](https://www.php.net/manual/en/class.pdostatement.php), not an array. You need to call [`fetch()`](https://www.php.net/manual/en/pdostatement.fetch.php) to retrieve results. – kmoser May 08 '22 at 20:02
  • @kmoser , $conn->query('SELECT * FROM `bucks-orders`') works and retrieves the results fine. – VaneStackoverflow May 08 '22 at 20:06
  • @Jaquarh I am already using md5(uniqid(mt_rand(), true)), what I want to find out is how to check if a item with same id already exists and if it exists then repeat until its unique in the database. – VaneStackoverflow May 08 '22 at 20:10
  • @kmoser I also couldn't understand https://stackoverflow.com/questions/1467581/how-to-generate-unique-id-in-mysql – VaneStackoverflow May 08 '22 at 20:12
  • `UUID()` ([MySQL manual](https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_uuid)) will give you e.g. `d392d574-cf0b-11ec-a11b-482ae31302cf`. Whatever you do, you definitely don't want to fetch all your data and check if the ID exists yet. Imagine 1M rows. You could just `SELECT uid FROM whatever WHERE uid = '$uidCandidate` and see if results are returned. If not, use it. You could just use `hrtime(true)` (17 digit high-resolution time in nanoseconds) and append `mt_rand()`, the chances of collision are nearly non-existent, and you also get chronological IDs. – Markus AO May 08 '22 at 20:25
  • If you wanted something shorter and alphanumeric, e.g. `base_convert(hrtime(true) . mt_rand(100000,999999), 10, 36);` would give you `186569463382000480468` as `13dgtn4tqnxw8c` (21 vs. 14 chars). Be aware that maximum integer size in PHP (64-bit) is 9223372036854775807 (or 19 chars), so you can only append so many more digits to `hrtime()` output before you run into trouble if you use it as an integer. – Markus AO May 08 '22 at 20:32

1 Answers1

0

If you have access to the DB, try setting the field to 'unique'.

This should do the checking for having one entry matching one id, never duplicating.

It does not sound like you after a mathematically unique number but just a unique id per DB field

Info link:

https://www.w3schools.com/mysql/mysql_unique.asp

MisterG13
  • 110
  • 10
  • What's the difference between "mathematically unique" and "unique in the table"? – Markus AO May 08 '22 at 20:51
  • Unique in the table is just any value that doesn't exist already. Could be just "A". But using math would be something like UUID() a long string, unique because the chances of generating two of the same are almost non existent. – MisterG13 May 08 '22 at 21:13
  • I see, "mathematical" as in "generator/algorithm that yields likely unique values on each iteration". Otherwise, any given value that doesn't recur in a given set is "mathematically unique", including the "A" in [A, B, C]. – Markus AO May 09 '22 at 11:54