user3610108
user3610108

Reputation: 13

Why CREATE TABLE IF NOT EXISTS always returns false?

i don't know what i am doing wrong here but i have been trying to get this to work for hours. i just want it to create a table that doesn't exist. i made it as simple as i can make it and still it just returns false and doesn't change anything. please let me what i am doing wrong thank you in advance.

$conn=mysql_connect('localhost:3306', 'root', '');
mysql_select_db("test",$conn);

$sql = "CREATE TABLE IF NOT EXISTS 'works' (
   `autoPlace` int(11) unsigned NOT NULL auto_increment,
   `element` float(255) NOT NULL,
   `month` tinyint(4) NOT NULL ,
   `mday`   tinyint(4) NOT NULL ,
   `wday` char(12) NOT NULL ,
   `time` smallint(6) NOT NULL,
    PRIMARY KEY  (`autoPlace`)     
   ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";

$thisKeepsReturningFalse= mysql_query($sql); var_dump($thisKeepsReturningFalse);

Upvotes: 0

Views: 1714

Answers (3)

user3610324
user3610324

Reputation: 1

When you create a float element you must specify the decimals with a comma.And also the name of the table can't be with this format '' it must to be inside ``.

So in this example the query should be :

CREATE TABLE IF NOT EXISTS `works` 
(
    `autoPlace` int(11) unsigned NOT NULL auto_increment,
    `element` float(255,0) NOT NULL,
    `month` tinyint(4) NOT NULL ,
    `mday`   tinyint(4) NOT NULL ,
    `wday` char(12) NOT NULL ,
    `time` smallint(6) NOT NULL,
    PRIMARY KEY  (`autoPlace`)     
) ENGINE=MyISAM  DEFAULT CHARSET=utf8

Note how the float it's float(255,0) and not just 255. You must set this , 0 is the default

Upvotes: 0

Mark Miller
Mark Miller

Reputation: 7447

When I tried to execute your query I got this error:

Incorrect column specifier for column 'element'

The problem is float(255) is not a valid declaration

It needs to be float(x) where x<=53, or you can use float(x,y) - see mysql docs here

Also, as pointed out in the comments, you have single quotes around works. Remove them or replace them with backticks.

After fixing these errors, I was able to successfully execute your query.

Upvotes: 4

Phuc
Phuc

Reputation: 155

"CREATE" command in MySQL is not return value command so that it always return false(not like SELECT). To check if the command "CREATE" success or not, you can use "SHOW TABLES" to check after executing the "CREATE" command.

Upvotes: 0

Related Questions