Jump to content

Mysql Code Is Giving Weird Errors


zyra

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...