In php I have an INSERT query to database. This query add some issue to database. I want to check if issue was added fine (there are some cases when issue doesn't added - without any mistake reporting). How to check this?
I have an idea, that after i make INSERT query i should make a query, for example: SELECT ..... WHERE id=last_id. But how to get the last_id - how to get id of last INSERT query? (id is autoincrementing sql field)
Thank you
mysql_insert_id();
You may find the answer to this StackOverflow question useful:
SQL - INSERT and catch the id auto-increment value
mysql_insert_id()
More info at http://php.net/manual/en/function.mysql-insert-id.php and http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
mysql_insert_id function returns id of previously inserted record.
I assume you're working with MySQL
Related
I am running an update query using PDO. I am updating only four rows but I do not know the names of columns. I can easily get the count of rows using rowCount() but I also want to fetch the column name as well. Is there any function in PDO which will help for the same? If not then please tell me the other method for fetching this.
You don't need PDO, you can get the names with MySQL:
SHOW COLUMNS FROM `table`
Hope it helps
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: how to get last inserted ID of a table?
I'm writing a function to add data do a database, but I then want to immediately use that entry in my code.
I know I could query: SELECT id FROM table ORDER BY id DESC
But I wonder if there's any more direct way of doing it.
That mostly depends on the interface you're using to access MySQL.
If you're using PDO (recommended), you'd use PDO::lastInsertId(): http://us.php.net/manual/en/pdo.lastinsertid.php
If you're using MySQL, you'd use mysql_insert_id() http://php.net/manual/en/function.mysql-insert-id.php
If you're using MySQLi, you'd use mysqli_insert_id(): http://us.php.net/manual/en/mysqli.insert-id.php
Just simply call whichever function is appropriate right after your INSERT completes.
It's important to note that you likely do not want to fetch id out of the database using SELECT because if you have multiple writers, you may get the id that some other thread inserted. At best, you'll operate on the wrong data, and at worst, you could have serious security issues and/or corruption.
With PHP you can use mysql_insert_id() function.
select last_insert_id();
Doc
Maybe this could help:
Get the Unique ID for the Last Inserted Row
If you insert a record into a table
that contains an AUTO_INCREMENT
column, you can obtain the value
stored into that column by calling the
mysql_insert_id() function.
I am using REPLACE INTO query to insert in to table but after executing query by using mysql_query() function and if I use last_insert_id() it is only giving me 0 value.
why this is happening so? and how can I overcome this behavior.
:Pankaj
You could try using INSERT INTO ... ON DUPLICATE KEY UPDATE instead. It accomplishes the same thing as REPLACE INTO, but in a single server-side operation. REPLACE INTO can end up causing two operations: delete the old one, insert the new one.
But regardless of the query type, you do have to ensure that the table's primary key is an auto_increment field. last_insert_id() does not work properly otherwise.
REPLACE INTO doesn't seem to affect the vaue that can be obtained via last_insert_id().
It seems to be the expected behavior, judging from this :
LAST_INSERT_ID() give wrong value after REPLACE INTO query
LAST_INSERT_ID()
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.
Is there a way to insert into MySQL database using PHP PDO and return its id? Or I will just have to search fot its id after insertion?
$pdo_object->lastInsertId();
See documentation
Perhaps you're looking for PDO::lastInsertID?