doflamingo doflamingo - 1 month ago 8
MySQL Question

PHP - Does it need to fetch_array when select then insert

Hello I'm writing PHP and mysql. If I want to select then insert to database, I will


  • step1.Select from table

  • step2. mysqli_fetch_array and contain to variable such as $foo

  • step3. INSERT INTO MyTable (firstname)
    VALUES ('$foo')"



Is it possible that I select then insert without fetch from database?

Answer

If you want to do this via mysql you can use

INSERT INTO Table2(field1, field2) 
SELECT old_field1 as field1, old_field2 as field2 FROM Table1

Just be sure that you match fieldnames with "as".