How to create a table in PHP with MySQL?

create a table with php and mysql using SHOW CREATE TABLE

  • I'm trying to create an exact duplicate of another table via php by using SHOW CREATE TABLE. I only want to change the table name, but I haven't figured out how to do that yet. If the old table's name is table_1, I want the new one to be table_2. This didn't work. Didn't really expect it to, but that's how far I got: $t = $DB->fetch("SHOW CREATE TABLE table_1"); $t[0] = "table_2"; $DB->query($t[1]);

  • Answer:

    The following query will create new table, column attributes and indexes will also be copied. CREATE TABLE new_table_name LIKE old_table_name; And if you also want the rows copied, then execute following query after executing the above INSERT INTO new_table_name SELECT * FROM `old_table_name`; http://www.mysqlfaqs.net/mysql-faqs/Data-Back-Up/How-to-create-duplicate-table-in-MySQL http://www.tech-recipes.com/rx/1487/copy-an-existing-mysql-table-to-a-new-table/

domino at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

<?php $result=mysql_query("SHOW TABLES"); $oldTableName="TABLE1"; $found=false; while($row=mysql_fetch_array($result)) { if($row[0]==$oldTableName) { $found=true; break; } } if($found) { //TODO Code for table1 } else { //TODO Code for table2 } ?>

Akhil Thayyil

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.