I'm trying to create following table . query works in phpmyadmin but doesn't work in php script. pls point out mistake in my code. Thank You
$sql = "CREATE TABLE IF NOT EXISTS `portal` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`message_sid` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`number` varchar(255) NOT NULL,
`message` text NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`status` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1";
if (mysqli_query($conn, $sql)) {
echo "Table portal created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
I'm getting this error:
You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for the
right syntax to use near '`id` int(255) NOT NULL AUTO_INCREMENT,
`message_sid` varchar(255) NOT NULL' at line 2
I have done this by checking if table exists in php, on false create a new table
creating table would be best to export an already existing one with phpmyadmin and then just use it as query syntax
Take the single quotes off of your table and row names
also if you make the id a primary key auto-increment, do not make it not null, this will cause a error.
I am unsure why the down votes....
CREATE TABLE IF NOT EXISTS portal (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
message_id VARCHAR(30) NOT NULL,
name VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
number VARCHAR(50) NOT NULL,
message text NOT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(50) NOT NULL )ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
This will work
I am following a codeiniter tutorial, which tells me to make a database with the following code (using mysql):
$sql = "create database login;
CREATE TABLE IF NOT EXISTS `user_login` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ";
I try to run this, using my own php code, but i keep getting this error, and have no idea how to solve it:
Error creating table: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `user_login` ( `id` int(11) NOT NULL AUTO_INCREM' at line 2
No idea what it could be.
create database login;
and
CREATE TABLE IF NOT EXISTS `user_login` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)
are two commands which are to be executed separately. Have the database created before you try to create the table - either via a SQL client or by executing something like:
$sql1 = 'create database login;';
$sql1->execute();
$sql2 = 'CREATE TABLE IF NOT EXISTS `user_login`...';
$sql2->execute();
I'm trying to make some SQL commands in XAMPP. My query returns following error:
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'CREATE TABLE IF NOT EXISTS kayttajat ( id INT(10) NOT NULL
AUTO_INCREMENT, tunn' at line 2
I can't find the syntax error. And when I run the exact same command by copy-paste in command line, it works. So do I need some different syntax in PHP code?
Also if I remove the first command, the error message moves to [...] right syntax to use near 'CREATE TABLE IF NOT EXISTS rivit [...]. If I remove second command the error comes from third command and so on. I really don't understand where the error is.
$query='
CREATE DATABASE IF NOT EXISTS asdgfhj;
CREATE TABLE IF NOT EXISTS kayttajat
(
id INT(10) NOT NULL AUTO_INCREMENT,
tunnus1 varchar(32) NOT NULL,
tunnus2 varchar(32) NOT NULL,
nimi varchar(32),
nimi2 varchar(32),
oikeus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS rivit
(
id INT(10) NOT NULL AUTO_INCREMENT,
sivu INT(10) NOT NULL,
kayttaja INT(10) NOT NULL,
sana varchar(500),
kommentti varchar(1000),
aika TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
muutos TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY(id)
);
CREATE TABLE IF NOT EXISTS sivut
(
id INT(10) NOT NULL AUTO_INCREMENT,
nimi varchar(32) NOT NULL,
ohje varchar(2000) NOT NULL,
salaisuus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY(id)
);
';
$mysqli->query($query) or die($mysqli->error);
The query() method is designed to execute a single query not multiple ones.
What you are looking for is multi_query() to execute multiple queries separated by a semicolon.
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* execute multi query */
if ($mysqli->multi_query($query)) {
...
}
You should use multi_query if you want execute many queries with one command.
Also you don't select database which will used for insert statements.
You should add asdgfhj prefix for all tables or use USE asdgfhj after create table statement.
example with prefix:
<?php
$query='
CREATE DATABASE IF NOT EXISTS asdgfhj;
CREATE TABLE IF NOT EXISTS asdgfhj.kayttajat
(
id INT(10) NOT NULL AUTO_INCREMENT,
tunnus1 varchar(32) NOT NULL,
tunnus2 varchar(32) NOT NULL,
nimi varchar(32),
nimi2 varchar(32),
oikeus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY (id)
);
';
$mysqli->multi_query($query) or die($mysqli->error);
example with use statement:
<?php
$query='
CREATE DATABASE IF NOT EXISTS asdgfhj;
USE asdgfhj;
CREATE TABLE IF NOT EXISTS kayttajat
(
id INT(10) NOT NULL AUTO_INCREMENT,
tunnus1 varchar(32) NOT NULL,
tunnus2 varchar(32) NOT NULL,
nimi varchar(32),
nimi2 varchar(32),
oikeus INT(10) NOT NULL DEFAULT 1,
PRIMARY KEY (id)
);
';
$mysqli->multi_query($query) or die($mysqli->error);
<?php
$ll=mysql_query("CREATE TABLE IF NOT EXISTS p (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`colum` varchar(255) NOT NULL DEFAULT '',
`ord` varchar(255) NOT NULL DEFAULT '',
`tex` varchar(255) NOT NULL DEFAULT '',
`search` varchar(255) NOT NULL DEFAULT '',
`count` varchar(255) NOT NULL DEFAULT '',
`order` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;INSERT INTO p (title,colum,ord,tex,search,count,order) VALUES ('$a','$b','$c','$d','$f','$h','$g');") or die(mysql_error()) ;
if($ll){
echo "insert AND CREATE ";}
else {echo "fail"; }
?>
I am working in a php language . In this page if the table is not created 1st create it and then insert the values into the column
After creating the table , I am inserting the values into the table but it showing me the error in the insert query
I am getting a this 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 'INSERT INTO p VALUES ('count','name','asc','1','search','count','order ')' at line 11
what am i doing wrong
I am not absolutly sure if that is the problem but you are passing 7 values in your INSERT statement while your table deffinition has 8 fields.
I assume you are doing that because 'id' field is autoincremental. However, if that is the case, you should specify which columns correspond with your values in the insert statement:
INSERT INTO $p (title, column, ord, ...) VALUES ('$a','$b','$c','$d','$f','$h','$g')
Here is a dry run seperating the two statements - should work:
<?php
$querys[]="CREATE TABLE IF NOT EXISTS $p (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL DEFAULT '',
`colum` varchar(255) NOT NULL DEFAULT '',
`ord` varchar(255) NOT NULL DEFAULT '',
`tex` varchar(255) NOT NULL DEFAULT '',
`search` varchar(255) NOT NULL DEFAULT '',
`count` varchar(255) NOT NULL DEFAULT '',
`order` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1";
$querys[]="INSERT INTO $p VALUES (null,'$a','{$b}','{$c}','{$d}','{$f}','{$h}','{$g}')";
foreach($querys as $sql) {
$ll = mysql_query($sql);
$error=mysql_error();
if($error!='') {
print $sql."\n";
die($error);
}
}
?>
INSERT INTO p (title,colum,ord,tex,search,count,order) VALUES ('$a','$b','$c','$d','$f','$h','$g')
should be
INSERT INTO p (`title`,`colum`,`ord`,`tex`,`search`,`count`,`order`) VALUES ('$a','$b','$c','$d','$f','$h','$g')
Hence order is a mysql keyword
you have ended the insert statement with a " double quote but not started you can write either
INSERT INTO p (title,colum,ord,tex,search,count,order) VALUES ('$a','$b','$c','$d','$f','$h','$g');
or you can write
("INSERT INTO p (title,colum,ord,tex,search,count,order) VALUES ('$a','$b','$c','$d','$f','$h','$g')");
try it in your code
note - please do not use mysqli and mysql in same code, rather you can use mysqli PDO
here is the 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 'order ASC LIMIT 100' at line 1.
here is the query passed to PDO for a prepared execute
SELECT * FROM web_menu_items WHERE menuId = ? ORDER BY order ASC LIMIT 100
here are the variables passed to the prepared execute
array(0 => "1")
here is the table structure
CREATE TABLE IF NOT EXISTS `web_menu_items` (
`id` int(11) NOT NULL auto_increment,
`menuId` int(11) NOT NULL,
`order` int(11) NOT NULL,
`requiredAccess` int(11) NOT NULL,
`hideIfNotAccess` int(11) NOT NULL,
`label` varchar(128) NOT NULL,
`link` varchar(128) NOT NULL,
`tagId` varchar(32) NOT NULL,
PRIMARY KEY (`id`),
KEY `menuId` (`menuId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
what am I not seeing here? why is this a syntax error?
order is a reserved word in mySQL.
Put backticks around it:
ORDER BY `order` ASC LIMIT 100
or consider renaming the field.