Can't figure out this MySQL syntax error - php

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.

Related

SQL syntax error in PHP code, even though command runs in command line

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

PDOException error in mysql SQL syntax [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I am trying to create 2 tables in the same MySQL database with a PHP-script:
table 'user' with primary key 'user_id' and table 'order' with primary key 'order_id' and foreign key 'user_id' from the 'user' table (1 to many relationship).
Table user creates successfully without problems:
$sql="CREATE TABLE user(
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
type ENUM('member','admin') NOT NULL,
username VARCHAR(30) NOT NULL,
email VARCHAR(80) NOT NULL,
pass VARBINARY(32) NOT NULL,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
date_expires DATE NOT NULL,
date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
date_modified TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (user_id),
UNIQUE (username),
UNIQUE (email)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
However, I am not able to create table order:
$sql="CREATE TABLE order(
order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
transaction_id VARCHAR(19) NOT NULL,
payment_status VARCHAR(15) NOT NULL,
payment_amount DECIMAL(6,2) UNSIGNED NOT NULL,
payment_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (order_id),
FOREIGN KEY (user_id) REFERENCES user (user_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
I get the following error:
Error creating table: 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( order_id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id ' at line 1
Already checked the syntax and cannot find the mistake. Could you please advise what went wrong? Thanks a lot.
You need to escape reserved words like order with backticks
CREATE TABLE `order` ( ...
or better use another name instead.
order is keyword used by mysql like (select from tbl_name order by id ASC) so for escaping from using keywords you have to use quotes `` to avoid my sql error
so your query should by
$sql="CREATE TABLE `order` (
order_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
transaction_id VARCHAR(19) NOT NULL,
payment_status VARCHAR(15) NOT NULL,
payment_amount DECIMAL(6,2) UNSIGNED NOT NULL,
payment_date_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (order_id),
FOREIGN KEY (user_id) REFERENCES user (user_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8";
enjoy :D

how do i can use # symbol in sqlquery

delimiter $$
CREATE TABLE `users` (
`u_id` int(10) NOT NULL AUTO_INCREMENT,
`ufirstname` varchar(30) NOT NULL,
`ulastname` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`pswd` varchar(255) DEFAULT NULL,
`confirmpswd` varchar(255) DEFAULT NULL,
`mobileno` int(12) DEFAULT NULL,
PRIMARY KEY (`u_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8$$
select u_id,ufirstname,mobileno whrer email=test#gmail.com
and i am getting an error:
Error Code: 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 'where' at line 1
please help me
You are missing your FROM clause, you incorrectly spelled where and you didn't surround your string in quotes:
SELECT u_id, ufirstname, mobileno
FROM users
WHERE email = 'test#gmail.com'
Do this:
select u_id,ufirstname,mobileno from users where email='test#gmail.com'
You need to quote the string value
put it in quotes as a String is supposed to
SELECT u_id, ufirstname, mobileno FROM users WHERE email = 'test#gmail.com'

Delete data from two tables with INNER JOIN

I want to delete data from two tables with one SQL query according to datetime_lastactive and if the IP addresses is matching your own. But I'm getting this error message when I try out the following SQL query:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: 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 'INNER JOIN visitors_main WHERE information_ipaddress = '123.123.123.123' A' at line 2' in ...
DELETE FROM visitors_list
INNER JOIN visitors_main
WHERE information_ipaddress = :ipaddress
AND datetime_lastactive < NOW() - INTERVAL 3 HOUR
The tables looks like this:
CREATE TABLE IF NOT EXISTS `visitors_list` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`id_visitor` int(10) DEFAULT '0',
`id_user` int(10) DEFAULT '0',
`data_filename` text NOT NULL,
`data_filename_get` text NOT NULL,
`data_useragent` text NOT NULL,
`datetime_lastactive` datetime NOT NULL,
`information_ipaddress` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
CREATE TABLE IF NOT EXISTS `visitors_main` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`id_user` int(10) DEFAULT '0',
`data_coordinates` varchar(25) NOT NULL,
`datetime_firstvisit` datetime NOT NULL,
`checkbox_anonymous` tinyint(4) DEFAULT '0',
`checkbox_tiecoordinates` tinyint(4) DEFAULT '0',
`checkbox_nogps` tinyint(4) DEFAULT '0',
`information_ipaddress` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
)
How can I make this work?
Try this instead:
DELETE l, m
FROM visitors_list AS l
INNER JOIN visitors_main AS m ON l.information_ipaddress = m.information_ipaddress
WHERE l.information_ipaddress = :ipaddress
AND l.datetime_lastactive < NOW() - INTERVAL 3 HOUR;
DELETE v1,v2
FROM visitors_list v1
INNER JOIN visitors_main v2 ON v1.id_visitor = v2.id
WHERE v1.information_ipaddress = :ipaddress
AND v1.datetime_lastactive < NOW() - INTERVAL 3 HOUR;

CREATE TABLE SQL statement won't run

When I try to run this statement:
CREATE TABLE 'score_table'
(
'name' text NOT NULL,
'email' text NOT NULL,
'company' text NOT NULL,
'score_total' bigint(11) NOT NULL,
'score_string' longtext NOT NULL,
'id' int(11) NOT NULL auto_increment,
'date' timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY ('id')
)
ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
I get 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 ''score_table'
('name' text NOT NULL, 'email' text NOT NULL, 'company' text NOT N' at line 1
And I have no idea what's wrong, any help would be greatly appreciated!
Table and field names need to be put in backticks instead of single quotes (or no quotes at all):
CREATE TABLE `score_table`.....
Use backticks and not single quotes:
Your SQL statement should be:
CREATE TABLE score_table (
`name` text NOT NULL,
`email` text NOT NULL,
`company` text NOT NULL,
`score_total` bigint(11) NOT NULL,
`score_string` longtext NOT NULL,
`id` int(11) NOT NULL auto_increment,
`date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`))
ENGINE= MyISAM DEFAULT CHARSET=latin1;
You shouldn't enclose your table name in single quotes like that.
CREATE TABLE `score_table` (`name` text NOT NULL, `email` text NOT NULL, `company` text NOT NULL, `score_total` bigint(11) NOT NULL, `score_string` longtext NOT NULL, `id` int(11) NOT NULL auto_increment, `date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
As mentioned earlier table name and field names must not be in Quotes so avoid them i edited your query try this and also, if you are using Magic Quotes avoid using them as well(just avoid Magic quotes in your current scenario to find if you are doing it all correct.)
CREATE TABLE score_table (name text NOT NULL, email text NOT NULL, company text NOT NULL, score_total bigint(11) NOT NULL, score_string longtext NOT NULL, id int(11) NOT NULL auto_increment, date timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, PRIMARY KEY (id) ) ENGINE=MYISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

Categories