XJTRy Posted February 18, 2008 Share Posted February 18, 2008 Well I am successfully creating a table with the script below. $query = "CREATE TABLE user ( Id int(6) NOT NULL auto_increment, Date varchar(10) NOT NULL, AC_Make varchar(15) NOT NULL, AC_ID varchar(10) NOT NULL, Legs varchar(15) NOT NULL, Route varchar(30) NOT NULL, Duration varchar(10) NOT NULL, Day_Land varchar(5) NOT NULL, Night_Land varchar(5) NOT NULL, Instrument varchar(5) NOT NULL, Approaches varchar(5) NOT NULL, Night varchar(5) NOT NULL, XC varchar(5) NOT NULL, PIC varchar(5) NOT NULL, Dual varchar(5) NOT NULL, Remarks varchar(30) NOT NULL, PRIMARY KEY (id) )"; $result = mysql_query($query); However, now I want to change the table name as well. I'm changing the name because I am working step by step to eventually creating custom tables. With the script above the same script is created time after time. Here's the script I tried that returns an error: $query = "CREATE TABLE user ( Id int(6) NOT NULL auto_increment, Date varchar(10) NOT NULL, AC_Make varchar(15) NOT NULL, AC_ID varchar(10) NOT NULL, Legs varchar(15) NOT NULL, Route varchar(30) NOT NULL, Duration varchar(10) NOT NULL, Day_Land varchar(5) NOT NULL, Night_Land varchar(5) NOT NULL, Instrument varchar(5) NOT NULL, Approaches varchar(5) NOT NULL, Night varchar(5) NOT NULL, XC varchar(5) NOT NULL, PIC varchar(5) NOT NULL, Dual varchar(5) NOT NULL, Remarks varchar(30) NOT NULL, PRIMARY KEY (id) )"; $result = mysql_query($query); $query = 'ALTER TABLE user change user1' <-----------------------------------------Heres the added query $result = mysql_query($query); include 'closedb.php'; Quote Link to comment Share on other sites More sharing options...
soycharliente Posted February 18, 2008 Share Posted February 18, 2008 ALTER TABLE old_name RENAME new_name; Taken from http://dev.mysql.com/doc/refman/4.1/en/alter-table.html Quote Link to comment Share on other sites More sharing options...
XJTRy Posted February 19, 2008 Author Share Posted February 19, 2008 I just tried the following with no success: It returns an error stating Parse error: parse error, unexpected T_STRING. $query = "CREATE TABLE user ( Id int(6) NOT NULL auto_increment, Date varchar(10) NOT NULL, AC_Make varchar(15) NOT NULL, AC_ID varchar(10) NOT NULL, Legs varchar(15) NOT NULL, Route varchar(30) NOT NULL, Duration varchar(10) NOT NULL, Day_Land varchar(5) NOT NULL, Night_Land varchar(5) NOT NULL, Instrument varchar(5) NOT NULL, Approaches varchar(5) NOT NULL, Night varchar(5) NOT NULL, XC varchar(5) NOT NULL, PIC varchar(5) NOT NULL, Dual varchar(5) NOT NULL, Remarks varchar(30) NOT NULL, PRIMARY KEY (id) )"; $result = mysql_query($query); $query = 'ALTER TABLE user RENAME user1' $result = mysql_query($query); include 'closedb.php'; Quote Link to comment Share on other sites More sharing options...
phpSensei Posted February 19, 2008 Share Posted February 19, 2008 What line? Quote Link to comment Share on other sites More sharing options...
XJTRy Posted February 19, 2008 Author Share Posted February 19, 2008 $query = 'ALTER TABLE user RENAME user1' $result = mysql_query($query); <--------------------------------------This one. Quote Link to comment Share on other sites More sharing options...
XJTRy Posted February 19, 2008 Author Share Posted February 19, 2008 I figured it out at dev.mysql.com. I was using the wrong syntax. ALTER TABLE is valid for many functions but not renaming the table. Instead there is a RENAME TABLE [] TO [] syntax as well. Here's the correct syntax abbreviated FYI.. $query='CREATE TABLE testing ( Date varchar(10) NOT NULL )'; $result= mysql_query($query); $query='RENAME TABLE testing TO testingone'; $result= mysql_query($query); include 'closedb.php'; Quote Link to comment Share on other sites More sharing options...
revraz Posted February 19, 2008 Share Posted February 19, 2008 You were also missing a semi colon $query = 'ALTER TABLE user RENAME user1'; <-- Quote Link to comment Share on other sites More sharing options...
XJTRy Posted February 19, 2008 Author Share Posted February 19, 2008 Now I am having trouble with renaming the table via variable. The following yeilds a table called '$name', but I wanted it to call the $name variable which should name is 'ryan'. Any ideas? $query='CREATE TABLE testing ( Date varchar(10) NOT NULL )'; $result= mysql_query($query); $name= 'ryan'; $query='RENAME TABLE testing TO $name'; $result= mysql_query($query); include 'closedb.php'; Quote Link to comment Share on other sites More sharing options...
revraz Posted February 19, 2008 Share Posted February 19, 2008 You need double quotes around your string. $query="RENAME TABLE testing TO $name"; Quote Link to comment Share on other sites More sharing options...
XJTRy Posted February 19, 2008 Author Share Posted February 19, 2008 You need double quotes around your string. $query="RENAME TABLE testing TO $name"; Worked. Thanks a lot! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.