PHP Code to create table.

$create_q = 'CREATE TABLE IF NOT EXISTS `' . $job_id . '` (
  `id` int(11) NOT NULL auto_increment,
  `mobile` bigint(20) NOT NULL,
  `routeID` int(11) NOT NULL,
  `status` varchar(20) NOT NULL default `Sent`,
  `handler` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ';

Mysql 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 '`Sent`, `handler` varchar(20) NOT NULL, PRIMARY KEY (`id`) )' at line 5

how to fix this error?

Member Avatar for diafol

change from

default `Sent`

to

 default 'Sent'

This is a string not a database entity like table or field.

still it's displaying the following error.

PHP Parse error:  syntax error, unexpected 'Sent' (T_STRING) in
Member Avatar for diafol

Your problem is with your concatentation. This works fine for me:

CREATE TABLE IF NOT EXISTS `me` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `mobile` BIGINT(20) NOT NULL,
  `routeID` INT(11) NOT NULL,
  `status` VARCHAR(20) NOT NULL DEFAULT 'Sent',
  `handler` VARCHAR(20) NOT NULL,
  PRIMARY KEY (`id`)
)

Do this:

    $create_q = "CREATE TABLE IF NOT EXISTS `$job_id` (
  `id` int(11) NOT NULL auto_increment,
  `mobile` bigint(20) NOT NULL,
  `routeID` int(11) NOT NULL,
  `status` varchar(20) NOT NULL default 'Sent',
  `handler` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
  ) ";

Your issue was that you cannot include a single quote (') within a single quoted string without escaping it (\) first. This would have been allowed too:

$create_q = 'CREATE TABLE IF NOT EXISTS `' . $job_id . '` (
  `id` int(11) NOT NULL auto_increment,
  `mobile` bigint(20) NOT NULL,
  `routeID` int(11) NOT NULL,
  `status` varchar(20) NOT NULL default \'Sent\',
  `handler` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.