J
J
Jony13372016-11-10 22:23:52
PHP
Jony1337, 2016-11-10 22:23:52

How to fix glitches with SELECT MAX(`id`)?

There is a code

$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT MAX(`id`) FROM `ia`";
$result = $conn->query($sql);
$row = $result->fetch_row();
$firstField = $row[0];
$nextID = $firstField +1 ;

when there is a request from two people at once, we have the first request to the database with id = $nextID data is added then the request of the second person rewrites the information of the first one with the same id , this happens rarely, but still how to do normalino?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene Volf, 2016-11-10
@Jony1337

I'm not sure if this will help 100%, I haven't tested the logic of this function, but it's worth a try:
In other words:
*I didn't notice right away... But after that with autoincrement it won't be superfluous.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question