I am creating a dynamic table using mysql query table name should be unique that's why i have added user id to table name.
In Controller:
$last_id = $this->User->getLastInsertID();
//$table_name = 'hello_'.$last_id.'_tutors';
// debug($table_name);
$this->User->query("CREATE TABLE IF NOT EXISTS `post_`.$last_id.`_tutors` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`user_id` int(8) NOT NULL,
`tutor_name` varchar(50) NOT NULL,
`tutor_email` varchar(50) NOT NULL,
`tutor_number` varchar(50) NOT NULL,
`tutor_gender` varchar(50) NOT NULL,
`tutor_address` varchar(100) NOT NULL,
`city_id` int(11) NOT NULL,
`area_id` int(11) NOT NULL,
`matric` varchar(200) NOT NULL,
`inter` varchar(200) NOT NULL,
`graduation` varchar(200) NOT NULL,
`masters` varchar(200) NOT NULL,
`diploma` varchar(200) NOT NULL,
`other_education` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=67 ;
");
it gave syntax error like:
Syntax error or access violation: 1064 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 '.75._tutors ( tutor_name
varchar(50) NOT NULL, tutor_email var' at line 1
if anyone may help. Thanks in advance.
Try using
$this->User->query("CREATE TABLE IF NOT EXISTS `post_".$last_id."_tutors` (
Related
I am trying to create a table in a database in phpMyAdmin and I keep getting the "a symbol name was expected" error and I cannot figure out what is going on. Is my syntax wrong? I am new to this and I'm at a loss.
Column names, table names should be surrounded by backticks(``)
CREATE TABLE IF NOT EXISTS `sales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`item` varchar(50) NOT NULL,
`date` varchar(50) NOT NULL,
`amount` int(11) NOT NULL,
PRIMARY KEY (`id`)
)
Or you can go without backticks as well:
CREATE TABLE IF NOT EXISTS sales (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
item varchar(50) NOT NULL,
date varchar(50) NOT NULL,
amount int(11) NOT NULL,
PRIMARY KEY (id)
)
Your code is wrong , So here is the correct Code :
CREATE TABLE IF NOT EXISTS `sales` (
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(50) CHARACTER SET utf8 NOT NULL,
`item` VARCHAR(50) CHARACTER SET utf8 NOT NULL,
`date` VARCHAR(50) NOT NULL,
`amount` int(11) NOT NULL
)
You used ' ' sign in your column properties. But mySQL allow you to use `` sign.
CREATE TABLE IF NOT EXISTS `sales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`item` varchar(50) NOT NULL,
`date` varchar(50) NOT NULL,
`amount` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
I have been having this reoccurring problem when attempting to import a .sql file into MySQL, Here is what I get
ERROR
SQL Query:
CREATE TABLE `ADMINS` (
`id` int(11) NOT NULL auto_increment,
`client_id` varchar(80) NOT NULL default '',
`client_pw` varchar(16) default NULL,
`client_school` varchar(16) default NULL,
`client_expdate` date default '0000-00-00',
`client_fullname` varchar(50) default NULL,
`webinfo` mediumtext,
`webinfodate` date default NULL,
`LastUpdate` timestamp(14) NOT NULL,
`user_activation_key` varchar(60) default NULL,
`user_email` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
MySQL Said:
#1064 - 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 '(14) NOT NULL,
`user_activation_key` varchar(60) default NULL,
`user_email' at line 10
Timestamps don't have lengths. Change:
timestamp(14)
to
timestamp
The syntax is not correct, as pointed by John you do not need to the length for timestamp datatype. In addition type=myisam is deprecated and its engine=myisam now.
CREATE TABLE `ADMINS` (
`id` int(11) NOT NULL auto_increment,
`client_id` varchar(80) NOT NULL default '',
`client_pw` varchar(16) default NULL,
`client_school` varchar(16) default NULL,
`client_expdate` date default '0000-00-00',
`client_fullname` varchar(50) default NULL,
`webinfo` mediumtext,
`webinfodate` date default NULL,
`LastUpdate` timestamp NOT NULL,
`user_activation_key` varchar(60) default NULL,
`user_email` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) engine=MyISAM AUTO_INCREMENT=2 ;
I am trying to create some tables on phpMyAdmin, but when I use the code below I am getting this error:
#1064 - 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 '(32) NOT NULL,
activated enum(0,1) NOT NULL,
PRIMARY KEY (id)
) E' at line 8
Here is my code:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`sign_up_date` date(32) NOT NULL,
`activated` enum(`0`,`1`) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=1atin1 AUTO_INCREMENT=1 ;
Try following code. I have resolved your mistakes in code
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`sign_up_date` date NOT NULL,
`activated` enum('0','1') NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
DATE data type takes no length
`sign_up_date` DATE NOT NULL
I have a very strange problem here. I am exporting a query through phpmyadmin and then when I run it using mysql_query I get an error saying that syntax of the query isn't correct.
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 'CREATE TABLE IF NOT EXISTS `wp_cfs_events` ( `event_id` int(11) NOT NULL AUTO_' at line 14
The same query runs perfectly fine using phpmyadmin. What could be the problem?
Here is the SQL query
CREATE TABLE IF NOT EXISTS `wp_cfs_certificates` (
`certificate_id` varchar(8) DEFAULT NULL,
`parent_name` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`phone_number` varchar(15) NOT NULL,
`emergency_number` varchar(15) NOT NULL,
`email` varchar(45) NOT NULL,
`certificate_price` enum('40','50') NOT NULL,
`certificate_order_time` datetime DEFAULT NULL,
`is_used` tinyint(1) DEFAULT '0',
UNIQUE KEY `certificate_id_UNIQUE` (`certificate_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `wp_cfs_events` (
`event_id` int(11) NOT NULL AUTO_INCREMENT,
`event_name` varchar(60) DEFAULT NULL,
`event_desc` varchar(2048) DEFAULT NULL,
`event_date` date DEFAULT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `wp_cfs_events` (`event_id`, `event_name`, `event_desc`, `event_date`) VALUES
(1, 'Culture Night', 'all about culture', '2013-05-16'),
(2, 'Sports Night', 'all about sports', '2013-05-31'),
(3, 'Random Night', 'randomness overloaded', '2013-06-15'),
(4, 'Winter Fest', 'the awesome winter fest', '2013-11-20'),
(5, 'Archived Event', 'this event has been occured in past', '2013-04-02');
CREATE TABLE IF NOT EXISTS `wp_cfs_parents` (
`parent_id` int(11) NOT NULL,
`parent_name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `wp_cfs_registrations` (
`registration_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`certificate_id` varchar(8) NOT NULL,
`event_id` varchar(45) DEFAULT NULL,
`parent_name` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`phone_number` varchar(15) NOT NULL,
`emergency_number` varchar(15) NOT NULL,
`email` varchar(45) NOT NULL,
`child_name` varchar(45) DEFAULT NULL,
`child_age` int(11) DEFAULT NULL,
PRIMARY KEY (`registration_id`),
UNIQUE KEY `certificate_id_UNIQUE` (`certificate_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `wp_cfs_registrations`
ADD CONSTRAINT `certificate_id` FOREIGN KEY (`certificate_id`) REFERENCES `wp_cfs_certificates` (`certificate_id`) ON DELETE NO ACTION ON UPDATE CASCADE;
Its because mysql_query() doesn't support multiple queries
Its mentioned in the official PHP documentation
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.
I want to create some table through PHP but it fails everytime, but the same code excutes perfectly when excuted through MYsql console or PHPMyAdmin
The SQLs are
$sql = <<<SQL_CODE
CREATE TABLE IF NOT EXISTS `bid` (
`aid` int(11) unsigned NOT NULL,
`uid` int(11) unsigned NOT NULL,
`name` varchar(20) NOT NULL,
`amount` smallint(6) unsigned NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS `item` (
`aid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`description` varchar(120) NOT NULL,
`img` int(11) unsigned NOT NULL,
`amount` smallint(6) unsigned NOT NULL,
`strtdate` date NOT NULL,
`enddate` date NOT NULL,
`uid` int(11) unsigned NOT NULL,
`uname` varchar(20) NOT NULL,
`uamount` int(11) unsigned NOT NULL,
PRIMARY KEY (`aid`)
);
CREATE TABLE IF NOT EXISTS `user` (
`uid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(40) NOT NULL,
`password` varchar(40) NOT NULL,
`nameF` varchar(20) NOT NULL,
`nameL` varchar(20) NOT NULL,
`sex` varchar(1) NOT NULL,
`img` int(11) unsigned NOT NULL,
`country` varchar(10) NOT NULL,
`state` varchar(20) NOT NULL,
`address` varchar(120) NOT NULL,
`code` varchar(8) NOT NULL,
`isAdmin` varchar(1) NOT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `email` (`email`)
);
SQL_CODE;
$sql2 = 'CREATE DATABASE `'.$db.'`;';
$sql3 = 'USE `'.$db.'`; ';
$sql4 = 'drop Database `'.$db.'``;';
if (!mysql_query($sql2)) echo mysql_error();
if (!mysql_query($sql3)) echo mysql_error();
//sleep(1);
if (!mysql_query($sql)) echo mysql_error();
echo $sql2.' '.$sql3.' '.$sql;
Error I'm getting
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 'CREATE TABLE IF NOT EXISTS item ( aid int(11) unsigned NOT NULL AUTO_INCREMENT,' at line 1CREATE DATABASE auction; USE auction; CREATE TABLE IF NOT EXISTS bid ( aid int(11) unsigned NOT NULL, uid int(11) unsigned NOT NULL, name varchar(20) NOT NULL, amount smallint(6) unsigned NOT NULL, time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); CREATE TABLE IF NOT EXISTS item ( aid int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(40) NOT NULL, description varchar(120) NOT NULL, img int(11) unsigned NOT NULL, amount smallint(6) unsigned NOT NULL, strtdate date NOT NULL, enddate date NOT NULL, uid int(11) unsigned NOT NULL, uname varchar(20) NOT NULL, uamount int(11) unsigned NOT NULL, PRIMARY KEY (aid));CREATE TABLE IF NOT EXISTS user ( uid int(11) unsigned NOT NULL AUTO_INCREMENT, email varchar(40) NOT NULL, password varchar(40) NOT NULL, nameF varchar(20) NOT NULL, nameL varchar(20) NOT NULL, sex varchar(1) NOT NULL, img int(11) unsigned NOT NULL, country varchar(10) NOT NULL, state varchar(20) NOT NULL, address varchar(120) NOT NULL, code varchar(8) NOT NULL, isAdmin varchar(1) NOT NULL, PRIMARY KEY (uid), UNIQUE KEY email (email));
From the mysql_query manual;
mysql_query() sends a unique query (multiple queries are not
supported)...
You need to split your triple CREATE TABLE statement into 3 separate statements and it will work.
I think that is a problem with multiple queries. I think that is solution.
http://php.net/manual/en/function.mysql-query.php
*mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier*
Just split them up and it will work.