I have little confusion about the Php PDO function: lastInsertID. If I understand correctly, it returns the last auto-incremental id that was inserted in the database.
I usually use this function when I execute a query that inserts a user in my database when I am creating the functionality of registering a user.
My question is that say I have a hundred people registering on my site at one point for example. And may be one user hit the 'Register' button a millisecond after another user. Then is there a chance that this function lastInsertId will return the id of another user that register just momentarily earlier?
May be what I am trying to ask is does the server handle one request at a time and go through a php file one at a time?
Please let me know about this.
Thank you.
Perfectly safe. There is no race condition. It only returns the last inserted Id from the pdo object that made the insert.
It is safe - it guarantees to return you a value from the current connection.
Related
I have little confusion about the Php PDO function: lastInsertID. If I understand correctly, it returns the last auto-incremental id that was inserted in the database.
I usually use this function when I execute a query that inserts a user in my database when I am creating the functionality of registering a user.
My question is that say I have a hundred people registering on my site at one point for example. And may be one user hit the 'Register' button a millisecond after another user. Then is there a chance that this function lastInsertId will return the id of another user that register just momentarily earlier?
May be what I am trying to ask is does the server handle one request at a time and go through a php file one at a time?
Please let me know about this.
Thank you.
Perfectly safe. There is no race condition. It only returns the last inserted Id from the pdo object that made the insert.
It is safe - it guarantees to return you a value from the current connection.
I have a site in laravel where a single page makes multiple database queries (select statements which can take a little while to run).
Is there a way to detect if a user:
Refreshes the page
Changes the parameters of the query
Closes the page
hence meaning that the result of the query is not needed, and therefore cancelling it or killing the process?
Note the database is mysql so I can from mysql workbench easily call kill {PID} to end a similar query manually.
Thanks!
Yest, this can all be detected easily with some JavaScript; there is an 'onunload' event that is fired when the user tries to leave or close the page, so that answers 1 and 3. Changing a field in a form is also easy to detect.
The hard problem will be to match the cancel request with the original query; somehow you have to know the PID of that query, and since MySQL functions generally don't return until they are done you can't ask for information like the PID.
You can detect if a user leaves or reloads a page with Javascript, catching the ONUNLOAD event (documentation here, SO related question here).
To check if query parameters change, you should specify if the user enters them manually in a form or any other way you are getting them, there's a different answer for any different method.
I have little confusion about the Php PDO function: lastInsertID. If I understand correctly, it returns the last auto-incremental id that was inserted in the database.
I usually use this function when I execute a query that inserts a user in my database when I am creating the functionality of registering a user.
My question is that say I have a hundred people registering on my site at one point for example. And may be one user hit the 'Register' button a millisecond after another user. Then is there a chance that this function lastInsertId will return the id of another user that register just momentarily earlier?
May be what I am trying to ask is does the server handle one request at a time and go through a php file one at a time?
Please let me know about this.
Thank you.
Perfectly safe. There is no race condition. It only returns the last inserted Id from the pdo object that made the insert.
It is safe - it guarantees to return you a value from the current connection.
I am building a log-in system which uses three tables in a mysql database (PHP) called users, sessions and log-ins. All tables have an auto-increment index. After a successful log-in happens, the user row is linked to a session row via the values stored in a new log-in row. I am wondering if mysqli_insert_id() is safe to use in this process. I am worried that if there is an error during the session row INSERT, the log-in row will receive an incorrect session index number and the user will get logged into the wrong session.
is this going to be a problem? If so, is there a good way to handle it?
That method will produce reliable results if:
The last INSERT operation succeeded.
The result is checked immediately after the INSERT succeeded on the same database connection.
Most of the time it will be sufficient to call INSERT and then fetch the ID of what was inserted as the next operation so long as you're using the same database handle.
A framework will do all of this for you automatically, so it's usually not something you should be concerned with.
If you want to ensure the integrity of your data & the operations that you perform, then I suggest you go with the "All or None" approach. This means that all your queries will pass individually or they will all fail, even if one fails. You can implement this using TRANSACTION & ROLLBACK features in MySQL. For more info, you may visit: http://www.tutorialspoint.com/sql/sql-transactions.htm
it's better if you use triggers
trigger will do a query when you trigger another
see this tutorial
I just finished programming a plattform on PHP that uses MySQL to save the results of a poll. I will be doing the poll with a group of 24 users that will submit the form at more or less the same time. So that the mysqli_query() function will be executed around 24 times at the same time.
Is this a problem?
Should I worry about the function blocking the access to the db, or is the mysqli_query() safe to use without having to worry about blocking?
In general no, unless you are doing something very interesting in the application logic that requires the data from another row. For example if you are using an ID it should be of the auto increamenting type, pretty standard stuff. You know you are ok if you are are only doing an insert and no select before hand and don't have a crazy stored proc on the database, which you likely do not.
mysql_queries will executed by order in which they arrived to mysql server.
You can don't worry about it