Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate mysql table "structure" using PHP only

Tags:

php

mysql

I have table called "users" and I want to make an exact copy as "users_2" as regard the structure only not the content.
I wanna do this using PHP only as I don't have an access to phpMyadmin or mysql console.
Do you have an idea how to do that ?

like image 846
taabouzeid Avatar asked Feb 06 '10 20:02

taabouzeid


People also ask

How can we copy only the structure of a table?

Right-click the table you want to copy in Database Explorer and select Duplicate Object. In the dialog that opens, select the destination db. Select to copy the table data or structure only. Specify the name of the new table, and click OK.

How do I copy a table structure in Sqlyog?

Select the table you want to copy and then select Table -> Copy Table to different Host/Database. At the appeared dialog box, make sure that the required table is default selected and if necessary, select other tables to be copied using treenode checkboxes.


2 Answers

After connecting to your database appropriately in php (mysql_connect):

mysql_query("create TABLE tablename like SRCTABLE");

like image 158
H. Green Avatar answered Sep 17 '22 18:09

H. Green


You can use the SQL SHOW CREATE TABLE users command, its result is a CREATE TABLE statement in which you can just replace users with users_2 and execute.

like image 44
Wim Avatar answered Sep 20 '22 18:09

Wim