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
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:
- How does MySQL reindex a table?Best solution by Stack Overflow
- How to create a Restful web service in .Net Using MySQL?Best solution by stackoverflow.com
- How to write a query for PHP and MySQL?Best solution by Stack Overflow
- What is Hash Table, how to create a hash table?Best solution by Stack Overflow
- How to create a table in gmail?Best solution by Web Applications
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.