Jump to content

Recommended Posts

Posted

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!

Posted

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.

Posted

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());
?>

Posted

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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...