Get the new record primary key ID from MySQL insert query?

You need to use the LAST_INSERT_ID() function: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id Eg: INSERT INTO table_name (col1, col2,…) VALUES (‘val1’, ‘val2’…); SELECT LAST_INSERT_ID(); This will get you back the PRIMARY KEY value of the last row that you inserted: The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned … Read more

Insert multiple rows WITHOUT repeating the “INSERT INTO …” part of the statement?

Your syntax almost works in SQL Server 2008 (but not in SQL Server 20051): CREATE TABLE MyTable (id int, name char(10)); INSERT INTO MyTable (id, name) VALUES (1, ‘Bob’), (2, ‘Peter’), (3, ‘Joe’); SELECT * FROM MyTable; id | name —+——— 1 | Bob 2 | Peter 3 | Joe 1 When the question was … Read more

Insert new item in array on any position in PHP

You may find this a little more intuitive. It only requires one function call to array_splice: $original = array( ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ ); $inserted = array( ‘x’ ); // not necessarily an array, see manual quote array_splice( $original, 3, 0, $inserted ); // splice in at position 3 // $original is now a … Read more

Solutions for INSERT OR UPDATE on SQL Server

don’t forget about transactions. Performance is good, but simple (IF EXISTS..) approach is very dangerous. When multiple threads will try to perform Insert-or-update you can easily get primary key violation. Solutions provided by @Beau Crawford & @Esteban show general idea but error-prone. To avoid deadlocks and PK violations you can use something like this: begin … Read more

“INSERT IGNORE” vs “INSERT … ON DUPLICATE KEY UPDATE”

I would recommend using INSERT…ON DUPLICATE KEY UPDATE. If you use INSERT IGNORE, then the row won’t actually be inserted if it results in a duplicate key. But the statement won’t generate an error. It generates a warning instead. These cases include: Inserting a duplicate key in columns with PRIMARY KEY or UNIQUE constraints. Inserting … Read more

How to insert an element after another element in JavaScript without using a library?

referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); Where referenceNode is the node you want to put newNode after. If referenceNode is the last child within its parent element, that’s fine, because referenceNode.nextSibling will be null and insertBefore handles that case by adding to the end of the list. So: function insertAfter(newNode, referenceNode) { referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); } You can test it … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)