mysql-insert-id

Alternative to "PDO::lastInsertId" / "mysql_insert_id"

I always hear that using "lastInsertId" (or mysql_insert_id() if you're not using PDO) is evil. In case of triggers it obviously is, because it could return something that's totally not the last ID that your INSERT created. $DB->exec("INSERT INTO example (column1) VALUES ('test')"); // Usually returns your newly created ID. // However w...

mysql_insert_id() failed to retrieve auto increment ID

$dml = "insert into ... "; mysql_query($dml,$con); $Id = isset($row) ? $row['id'] : mysql_insert_id($con); I saw the record is created,but just can't retrieve the Id. What's wrong? EDIT fixed,it's caused by $row. ...

Do you use mysql_error($con) or mysql_insert_id($con) to check insert result ?

$dml = "insert into table ..."; mysql_query($dml,$con); The above insert something into a table.then you can check if it succeeded by either if('' == mysql_error($con))... or if($id = mysql_insert_id($con))... What's your choice and reason? BTW,will the below still have $id fetched when running both of them,I've not tried yet: ...

I'm trying to populate a MySQL table with some data, but, mysqli won't let me insert every 10th statement

I want to initialise a 'ticket' table with some ticket IDs. To do this, I want to insert 120 ticket IDs into the table. However, at every 10th statement, MySQL tells me that the ID already exists and thus won't let me insert it. Here's my code: //make a query $insert_ticket_query = "INSERT INTO ticket (id) VALUES (?)"; $inse...

Mysql_insert_id with Doctrine

Hi! I have a couple of tables (mySQL) that i would like to update with the help of Doctrine. The products table id is auto-incrementing, and here's a brief description on what I would like to do: $prod = new Products(); $prod->type = '0'; $categ = new CategoriesToProducts(); $categ->cat = '111'; $categ->product = $...

getting mysql_insert_id() while using ON DUPLICATE KEY UPDATE with PHP

Hi-- I've found a few answers for this using mySQL alone, but I was hoping someone could show me a way to get the ID of the last inserted or updated row of a mysql DB when using PHP to handle the inserts/updates. Currently I have something like this, where column3 is a unique key, and there's also an id column that's an autoincremented ...

Question regarding mysql_insert_id() application in my code..

I have a page in which there is an if...else loop for processing of input data, this page is reverted two times when submitted, that is when a button called "Continue" is clicked and and second time "Add to cart".... Now in this case when Button "Continue" is clicked then the if loop is executed in which data is inserted in the Database,...

Php, MySql - Multiple DB connections and mysql_insert_id()

I have 2 database connections, and I want to get the last inserted ID from one of the connections. $old_database = mysql_connect('host', 'username', 'password'); mysql_select_db('database1', $old_database); $new_database = mysql_connect('host', 'username', 'password',true); mysql_select_db('database2', $new_database); $sql=mysql_query...

How bad is using SELECT MAX(id) in MYSQL instead of mysql_insert_id() in PHP?

Hi, Background: I'm working on a system where the developers seem to be using a function which executes a MYSQL query like "SELECT MAX(id) AS id FROM TABLE" whenever they need to get the id of the LAST inserted row (the table having an auto_increment column). I know this is a horrible practice (because concurrent requests will mess the...

php: mysql_insert_id() issues

Can the php function mysql_insert_id() return no result after processing the INSERT query in mysql db? Just to clarify. There was a script performing by cron on the production site. It contained a cycle for generating invoices for users. Each iteration consists of a INSERT db query and the mysql_insert_id() operation going right after t...

Using mysql_insert_id to insert a lot of data

I want to use PHP and MySQL to insert data into a data table and its associated many-to-many table. The auto-incrementing primary ID of the data table is used in the many-to-many table. So, I need to know the ID's of the data table to insert into the many-to-many table. My current thinking is that the best way to approach this is with...