Insert a PHP array into two MySQL tables

I am wondering how best to insert a multidimensional array of values ​​into a database - in particular, two tables? I created the following foreach loop that creates a query that will insert all the records in one table:

foreach($newPosts as $value) {
            if(!isset($postQuery)) {
                $postQuery = "INSERT INTO posts (primay_key, col1, col2, col3, col4) VALUES ('$value[0]', FROM_UNIXTIME($value[4]), '$value[2]', '$value[1]', '$value[3]')";
            } else {
                $postQuery .= "('$value[0]', FROM_UNIXTIME($value[4]), '$value[2]', '$value[1]', '$value[3]')";
            }
    }

I want to save one of the values ​​in a separate table, since the value is especially large and the string is formatted as Longtext. This value is also rarely requested when querying the database. I assume moving it to the second table will increase the query speed of the first table? It is right?

If you move this value to the second table, I want to link the tables with primary_key from the first table, which is the auto-increment value. How do I scroll this multidimensional array and insert my data into both tables, inserting the primary_key of the first table into the second? I know that I can use LAST_INSERT_ID () if I execute each request one at a time. Some updates insert hundreds of lines, so I don't want to do this.

Thanks in advance!

0
source share
1 answer

I assume moving it to the second table will increase the query speed of the first table? It is right?

Not necessary. This will affect performance.

  • ( , MySQL )
  • null ( )

, multi-inserts, 2 . , , , ( ) .

0

Source: https://habr.com/ru/post/1765574/


All Articles