PDO: Is lastInsertId() a separate query? - php

If I have this code:
<?php
$q = $sql->prepare("INSERT INTO `table` (row) VALUES ('1')");
$q->execute();
$lastid = $sql->lastInsertId(); // is this a 2nd query?
?>
Would it run as two separate SQL queries?
If so, is there a way to do it in one?

$lastid = $sql->lastInsertId();
it works like a query because it will select from database the last inserted id.
as the documentation said
Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver.

No, in fact it doesn't run an SQL query.
Here's the line from ext/pdo_msyql/mysql_driver.c:
char *id = php_pdo_int64_to_str(mysql_insert_id(H->server) TSRMLS_CC);
This is a call to the MySQL API, mysql_insert_id(). This internally accesses the last insert id as a property, not by running SQL.

Related

how to insert data into 2 different tables (php mysql)

2 Tables:
1. BOOKS_IN
BOOKS_in_ID,
DATE,
USERID
2. BOOKS_IN_DETAIL
BOOKS_in_ID,
BOOK_ID,
STOCK
BOOKS_in_ID is a primary key and i need BOOKS_in_ID is automatically insert into BOOKS_IN_DETAIL.
Here, is it possible to insert records into 2 table using single query?
thankyou for your advise.
You need to call the appropriate method to get the last inserted id.
Assuming you use PDO, you need to call the method lastInsertId. $books_in_id = $pdo->lastInsertId();
If you use mysqli_* extension that's $books_in_id = $mysqli->insert_id;
EDIT: if you use the mysql_* version ( which is deprecated), upgrade first to mysqli_* , or check in the documentation
You still need two INSERT statements, but it sounds like you want to get the IDENTITY from the first insert and use it in the second, in which case, you might want to look into OUTPUT or OUTPUT INTO: http://msdn.microsoft.com/en-us/library/ms177564.aspx
Src and possible duplicate of: SQL Server: Is it possible to insert into two tables at the same time?
You can also use LastInsertId() for PDO.
A small example:
$sql = "INSERT INTO city (`city`) VALUES ('Paris') ON DUPLICATE KEY UPDATE `city` = 'Paris";
$dbh->query($sql);
echo $dbh->lastInsertId();
Src: http://php.net/manual/en/pdo.lastinsertid.php
Or get the last insert ID in mysqli:
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);
printf ("New Record has id %d.\n", $mysqli->insert_id);
Src: http://php.net/manual/en/mysqli.insert-id.php
Do you have to stick to mysql? Because if you can use mysqli you can use multi_query(), which lets you execute multiple queries at once.
Link : http://php.net/manual/en/mysqli.quickstart.multiple-statement.php
No,Its not possible with only one INSERT query.
You can follow these steps
Write two different queries and execute them
Create Stored Procedure that execute two INSERT queried
For point One you can useLAST_INSERT_ID() function to add foreigh key

Could mysql_insert_id() get a value from another script's query

The manual for mysql_insert_id() says
Because mysql_insert_id() acts on the last performed query, be sure to
call mysql_insert_id() immediately after the query that generates the
value.
This statement does not indicate any scope. Is it a serverwide value? Could a server running many scripts or many instances of one script return a value for mysql_insert_id() which was not generated by the last insert performed by the script calling it?
The scope of mysql_insert_id() is the MySQL connexion. Not the user + password, but the actual connection for the current script. (Note that the MySQL connection can also be a parameter of mysql_insert_id)
If you close and re-open the MySQL connexion, mysql_insert_id() will not return the id of the id inserted in the previous one.
If an id has been inserted just after your adding, but by another script execution (I mean with another connexion) the mysql_insert_id() will return your ID, not the ID actually created after your but in another connection.
Example :
$c1 = mysql_connect($srv, $usr, $pwd);
$c2 = mysql_connect($srv, $usr, $pwd);
$sql = "INSERT INTO table1 (col1, col2) VALUES('x', 'y') "
mysql_query($sql, $c1); // first insert
mysql_query($sql, $c2); // second insert
$id1 = mysql_insert_id($c1);
$id2 = mysql_insert_id($c2);
$id1 will be the id inserted first, $id2 the id inserted after.
Yes, please see the following article: How to Get the Unique ID for the Last Inserted Row
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. It is not even changed if you update another AUTO_INCREMENT
column with a nonmagic value (that is, a value that is not NULL and
not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns
simultaneously from multiple clients is perfectly valid. Each client
will receive the last inserted ID for the last statement that client
executed.
The scope is per-connection. If each run of the script opens a separate connection (which is what usually happens), then they will have separate scopes.

Why doesn't PDO's Oracle driver implement lastInsertId()?

I get this error in PDO:
error: Message: PDO::lastInsertId() [pdo.lastinsertid]:
SQLSTATE[IM001]: Driver does not support this function: driver does
not support lastInsertId()
when trying to get last inserted id from an oracle database. I added the sequence string to the last insert id function but still not working. Google doesn't say much regarding this error on Oracle with PDO.
Oracle doesn't have autoincrement columns, so lastInsertId isn't supported in the same way as for MySQL. You have to implement the equivalent "by hand" using Oracle sequences.
Create an oracle sequence for every table that requires it, and use NEXTVAL to retrieve it whenever you need to do an insert, then use that value when inserting in the table.
$sh = $conn->prepare('SELECT uid_seq.NEXTVAL AS nextInsertID FROM DUAL');
$sh->execute();
$nextInsertId = $sh->fetchColumn(0);
$sh = $conn->prepare("INSERT INTO table (id, data) VALUES(?, 255)");
$sh->execute(array($nextInsertId));
In Oracle, you should have created an oracle sequence for tables which requires an auto increment column.
If you want to insert, then you can do it in the same time rather than query nextIncrementId and insert as below:
$sh = $conn->prepare("INSERT INTO table (id, data) VALUES(SEQUENCE_NAME.NEXTVAL, ?)");
$sh->execute(array($valueToBeInsertedInDataColumn));
If you just want to know the last insert id, then don't use nextval because whenever you call it, it increment the value to the next one. You should use CURRVAL for that purpose. Below is an example:
$sh = $conn->prepare("SELECT SEQUENCE_NAME.CURRVAL AS lastInsertId FROM DUAL");
$lastInserId = $sh->execute();
Debug: print_r($lastInserId);

PDOStatement::bindParam() not setting AI value from MySQL insert?

I have a simple insert statement using PDO php class.
the 'id' column is identity (doy) with autoincrement.
$statement = $db->prepare('INSERT INTO demographics (id,location_id,male,ethnicity_id,birthyear) VALUES (:id,:location_id,:male,:ethnicity_id,:birthyear)');
$statement->bindParam(':id',$demo->id,PDO::PARAM_INT,4);
$statement->bindParam(':location_id', $demo->locationid,PDO::PARAM_INT);
$statement->bindParam(':male',$demo->male,PDO::PARAM_BOOL);
$statement->bindParam(':ethnicity_id',$demo->ethnicityid,PDO::PARAM_INT);
$statement->bindParam(':birthyear',$demo->birthyear,PDO::PARAM_INT);
$statement->execute();
print_r($demo);
Even though the statement executes correctly (row is correctly written), $demo->id is null.
I have a very similar statement for a different table, and it correctly returns the id column.
Any thoughts?
If id is autoincremented, you don't need to specify it when inserting into the table. What value are you giving to $demo->id?
If you need the id of the inserted entry, you could retrieve it using PDO::lastInsertId, then set the object's $id field with the value.

Get autoincrement id after an insert query performed with a prepared statement

If I execute an insert query with a stored procedure with php and the mysqli_* functions, is there a way to retrieve the value of an autoincrement id field?
mysqli->insert_id does not seem to work.
Are you sure the last query you preformed was an INSERT?
mysqli->insert_id seems the proper answer:
Return Values
The value of the AUTO_INCREMENT field that was updated
by the previous query. Returns zero if
there was no previous query on the
connection or if the query did not
update an AUTO_INCREMENT value.
You could try to make a query to MySql like so:
SELECT LAST_INSERT_ID()
Not sure if it works with stored procedures though.
You could add this statement in your stored procedure after the insert:
SET #saved_id = LAST_INSERT_ID()
Then, execute this query after calling the procedure:
SELECT #saved_id
mysqli->insert_id (where mysqli represents your database connection)
must be used directly after the insert. If you run other queries on the same connection
before attempting to read insert_id you get 0 returned.

Categories