0

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?

2
Contributors
3
Replies
11
Views
1 Year
Discussion Span
Last Post by diafol
2

change from

default `Sent`

to

 default 'Sent'

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

Edited by diafol

0

still it's displaying the following error.

PHP Parse error:  syntax error, unexpected 'Sent' (T_STRING) in
2

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`)
) ';

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.