zyra Posted December 3, 2013 Share Posted December 3, 2013 So, I'm trying to make a code to create a table, and I'm really new to mySQL. So I used phpMyAdmin for the code, and it gave me: CREATE TABLE `$id` ( `Character` varchar(1) COLLATE latin1_general_cs NOT NULL, `Code` varchar(2) COLLATE latin1_general_cs NOT NULL, UNIQUE KEY `Character` (`Character`,`Code`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs But it didn't work right, so I tried fixing it and got: <?php mysql_connect("localhost", "zyra_*******", "*****") or die(mysql_error()); mysql_query("CREATE TABLE $id ( Letter varchar(1) COLLATE latin1_general_cs NOT NULL, Code varchar(2) COLLATE latin1_general_cs NOT NULL, UNIQUE KEY Character (Character,Code) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs") or die (mysql_error()); ?> But then it started giving me a weird error."You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( Character varchar(1) COLLATE latin1_general_cs NOT NULL, Code varchar(2) COLLAT' at line 1"And I have no idea what to do. Please help! Quote Link to comment Share on other sites More sharing options...
MDkate Posted December 3, 2013 Share Posted December 3, 2013 Without the backticks, PHP is assuming $id is a variable. The variable is empty and this creates a syntax error. You don't get any error when trying the other way? Edit: Try using mysql_query with apostrophes so the variable will not be parsed. I think PHP will still assume it's a variable. Quote Link to comment Share on other sites More sharing options...
Ice IT Support Posted December 4, 2013 Share Posted December 4, 2013 What MDkate is saying is correct. Use this code and notice the added ` marks and the PHP escape character \ <?php mysql_connect("localhost", "zyra_*******", "*****") or die(mysql_error()); mysql_query("CREATE TABLE `\$id` ( `Letter` varchar(1) COLLATE latin1_general_cs NOT NULL, `Code` varchar(2) COLLATE latin1_general_cs NOT NULL, UNIQUE KEY Character (Character,Code) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs") or die (mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
zyra Posted December 4, 2013 Author Share Posted December 4, 2013 Thanks! That helped! Oh, and is "character" a key word in mySQL because it made me change it. Quote Link to comment Share on other sites More sharing options...
Ice IT Support Posted December 4, 2013 Share Posted December 4, 2013 If it's between the ` marks, then MySQL interprets that as the name of a table or column. When you changed the names of the table to Letter and Code, the unique key statement at the bottom also should have been updated. I missed that. 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.