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

MySQL Conditional Insert

If your DBMS does not impose limitations on which table you select from when you execute an insert, try: INSERT INTO x_table(instance, user, item) SELECT 919191, 123, 456 FROM dual WHERE NOT EXISTS (SELECT * FROM x_table WHERE user = 123 AND item = 456) In this, dual is a table with one row only … Read more

How to insert multiple rows from array using CodeIgniter framework?

Assembling one INSERT statement with multiple rows is much faster in MySQL than one INSERT statement per row. That said, it sounds like you might be running into string-handling problems in PHP, which is really an algorithm problem, not a language one. Basically, when working with large strings, you want to minimize unnecessary copying. Primarily, … 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