0

I would like to know how to write this query in pdo

Here I want to copy the structure of old_table to new_table. I know to create table in PDO. I want to know how to write below mentioned query in PDO.

query : CREATE TABLE new_table LIKE old_table;

actual code I used:

$tempname=1001; //(its user registration id)

$keyword='temp_quesion%'; //(temp_question table already exists in db)

 $conn = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);
 $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );//Error Handling
 $sql =$conn->prepare("CREATE TABLE ".$tempname."_temp_question LIKE :refer "); 
 $sql->bindParam(':refer', $keyword);
   $sql->execute();

Here I want create temporary table to every user who login.

1 Answers1

0

Reading your comments I see that you want to copy the structure, if so you can do this in mysql:

CREATE TABLE new_table AS (SELECT * FROM old_table);

Which will create new table with the data from old table maintaining fields and values.

James McClelland
  • 555
  • 4
  • 16
  • By using this method table structure is get copied but primary key and auto increment other relations are not copied – user2416381 Oct 21 '14 at 07:43