This question already has answers here:
MySQL and PDO: Could PDO::lastInsertId theoretically fail?
(2 answers)
Closed 7 years ago.
I am hoping to not have to write another request to mysqli to get the id of an inserted row.
I need to know if PDO::lastInsertId is an asynchronous thread safe method.
Since this is a functionality question, i will provide a schematic instead of a library i wrote that handles PDO; However, I will provide if necessary.
SCHEMATIC
$connect = mysqli->connect
$is_inserted = $connect->insert(...)
if($is_inserted){
$last_insert_id = $connect->lastInsertId();
}
I need to make sure that a another user will not get someone elses insert_id if they are running the script simultaneously.
The docs for LAST_INSERT_ID() say:
For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client.
So, you should have no issues using $connect->lastInsertId().
Related
This question already has answers here:
Call to undefined method mysqli_stmt::get_result
(10 answers)
Closed 3 years ago.
I moved my php code to a new hosting which missing mysql_nd and a have more than 150 functions that use stmt->get_result() and fetch_assoc.
Is there a function that can do the same work of stmt->get_result() and fetch_assoc and give the exact results so I can fix the problem without having to modify all the functions?
You have two choices.
The first is to contact your provider indicating that you need MySql Native Driver.
The second is to review all your code as a PDO or alternatively MySqli (PDO Council).
In terms of time, with the second option we invest a little more, but for sure you will make your application more stable and secure. I would personally use it for the second hypothesis.
This question already has answers here:
MySQL and PDO: Could PDO::lastInsertId theoretically fail?
(2 answers)
Closed 8 years ago.
I've got a page calling a script via AJAX which could be asynchronously called multiple times simultaneously from the same parent page. This script uses require_once to access a configuration script with the database handler. Will the script call a unique instance of this handler each time, or if multiple copies are open will they all be using the same instance and causing undesirable results when I attempt to get the ID of the last insert from that instance of the script?
Just to be clear, this is the command I'm referencing:
$DBH->lastInsertId()
Thanks!
The documentation says
This method may not return a meaningful or consistent result across different PDO drivers, because the underlying database may not even support the notion of auto-increment fields or sequences.
That being said, I've never had a problem using lastInsertId with MySQL. It's always worked.
This ostensibly wraps a call to MySQL's LAST_INSERT_ID()
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client.
i.e. it should be safe to use even if you are facing frequent requests.
$DBH->lastInsertId() retrieves, in case of MySQL, the result of last_insert_id() function.
The last_insert_id() is session based, which means that if you have 2 PHP scripts running, you will only get the ones last_insert_id that were generated by each script.
The ID that was generated is maintained in the server on a
per-connection basis. This means that the value returned by the
function to a given client is the first AUTO_INCREMENT value generated
for most recent statement affecting an AUTO_INCREMENT column by that
client. This value cannot be affected by other clients, even if they
generate AUTO_INCREMENT values of their own. This behavior ensures
that each client can retrieve its own ID without concern for the
activity of other clients, and without the need for locks or
transactions.
MySQL and PDO: Could PDO::lastInsertId theoretically fail?
As noted in the documentation:
Note: This method may not return a meaningful or consistent result
across different PDO drivers, because the underlying database may not
even support the notion of auto-increment fields or sequences.
This question already has answers here:
How to send a SQL query to database in PHP without waiting for result
(3 answers)
Closed 9 years ago.
Firstly I'm using the deprecated mysql functions (instead of mysqli) knowingly, so please do not tell me I should change to mysqli.
My question is: if I want to do an INSERT or UPDATE and continue processing the PHP script immediately, without waiting for MySQL to complete the task, can I use mysql_unbuffered_query (is that what is does?) or if not, how can I achieve that?
Sorry to break this to you :)
If you use mysqli, with the mysqlnd driver, you can pass a MYSQLI_ASYNC option to the query() method. Unbuffered queries do not help here.
Later on you can use the poll() and reap_async_query() to get to the result.
You can use INSERT...DELAYED for asynchronous insertions (1). I do not believe you can do asynchronous UPDATE's without resorting to spawning another process (2).
(1) but is not available to InnoDB tables
(2) if sticking to the old mysql extension is an absolute requirement
This question already exists:
Closed 10 years ago.
Possible Duplicate:
SQL compatibility issue
I have asked 1-2 questions here at stackoverflow, but apparently i'm on the wrong way approaching!
The issue is, i got this thing. I connect to the sql server like this:
odbc_connect("Driver={SQL Server};Server=$host;Database=$database;",$uid, $passVal ) or die("Connection could not established");
It's supposed to use the database i've selected.
Now, here is how i do to select the table columns.
$result = mysql_query("DESCRIBE TABLE users");
$data = mysql_fetch_assoc($result);
print_r($data);
Now, please someone guide me where i'm wrong, it's my first approach with this.
You may try to act "sarcastic, ironic" all you want, it would be rude.
My question is, why doesn't it recognize the database? apparently, that's the issue.
Thanks
The odbc_ methods have nothing to do with the mysql_ methods. If you connect using odbc_connect, you need to use the odbc_ methods to query the database. Calling any mysql_ function makes it establish some default connection to the nearest default database, which is entirely separate from the previously established odbc_ connection.
Guess what, you cannot query an MS SQL server using the mysql API.
This question already has answers here:
PHP: multiple SQL queries in one mysql_query statement
(4 answers)
Closed 8 years ago.
These two PHP MySQL queries work.
mysql_query("DELETE FROM videos WHERE id='10';");
mysql_query("DELETE FROM comments WHERE videoId='10';");
This single query fails due to a MySQL syntax error pertinent to the latter DELETE operation.
mysql_query("DELETE FROM videos WHERE id='10';DELETE FROM comments WHERE videoId='10';");
I've stared hard and can't see the syntax error. What is it?
Not supported by mysql_query see How can I put two queries in one mysql_query? use http://docs.php.net/mysqli.multi-query
You cannot execute multiple queries with mysql_query. If you really want to (security risk!), use mysql_multi_query. (And you should use the newer mysqli_* functions). It's a good idea two embed those two calls in a transaction.
But this looks a lot like you really want to define foreign key constraints. I highly recommend them, if you are already using InnoDB.
Multiples queries are not supported in this function.
http://php.net/manual/en/function.mysql-query.php