Create TABLE in PHP using

160 views Asked by At

tables in my database where are some unusual letters from czech alphabet like ěščřžýáíé ignore my query. I think it could be solve via CHARACTER SET utf8 COLLATE utf8_czech_ci, but it looks like i dont know how to use it. Iam trying this code, but for sure, there is some problem:

        mysql_query("CREATE TABLE `".$userreg."` CHARACTER SET utf8 COLLATE utf8_czech_ci(
        id INT NOT NULL AUTO_INCREMENT, 
        PRIMARY KEY(id),
        title VARCHAR(30), 
        rating_estimate INT)")
        or die(mysql_error());

I have to create table in PHP code, because it depends on another variables. Browser shows "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 'id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), title VARCHAR(30), ' at line 2".

Any idea how to fix?

2

There are 2 answers

0
Hartmut Holzgraefe On BEST ANSWER
CHARACTER SET utf8 COLLATE utf8_czech_ci

needs to be at the end of the CREATE statement after the closing ')'

0
Ali MasudianPour On

You can use mysql_set_charset()

mysql_set_charset('utf8');//or utf8_czech_ci

Or:

mysql_query("SET character_set_results=utf8"); 

You can also ALTER your table:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

However, it is suggested to see Why shouldn't I use mysql_* functions in PHP?