Every time I try to run this MYSQL statement in phpMyAdmin I get this sytax 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 'comments,last_comment, '%d/%m/%Y %H:%i:%s') ASlast_comment
FROMposts' at line 7
Code:
"SELECT
`posts`.`post_id` AS `id`,
`posts`.`post_title` AS `title`,
LEFT(`posts`.`post_body`, 512) AS `preview`,
`posts`.`post_user` AS `user`,
DATE_FORMAT(`posts`.`post_date`,'%d/%m/%Y %H:%i:%s') AS `date`,
`comments`.`total_comments`,
DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
FROM `posts`
LEFT JOIN (
SELECT
`post_id`,
COUNT(`comment_id`) AS `total_comments`,
MAX(`comment_date`) AS `last_comment`
FROM `comments`
GROUP BY `post_id`
) AS `comments`
ON `posts`.`post_id` = `comments`.`post_id`
ORDER BY `posts`.`post_date` DESC";
Also, all of the tables are named correctly. So, that can be ruled out.
UPDATE:
Awesome, thanks. I added the quote on comments and the extra perimeter was meant to be a period not a comma.
DATE_FORMAT() accepts two parameters, not three. See the manual.
I suppose, you meant
`comments`.`total_comments`
instead?
`comments`,`last_comment`
should be
`comments`.`last_comment`
I.e. the comma should be a dot, otherwise it looks like you are trying to pass three parameters to DATE_FORMAT.
Related
I am trying to Insert data from a form with the use of a query. The query ( below ) has a WHERE clause to pick a position from visitorsystem.position.
$query = "INSERT INTO visitorsystem.employee(idNumber,name,surname,position,email)
VALUES ('$idNumber','$name','$surname',SELECT positionid FROM visitorsystem.position WHERE position LIKE '%$position%','$email')";
When executed the following error is given. I have tried adding quotes and single quotes around the SELECT...WHERE clause with no luck. Any ideas if the problem is with the query itself or the SELECT...WHERE clause ?
Error in query: 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 '','fdsf',SELECT positionid FROM visitorsystem.position WHERE
position LIKE '%inf' at line 2
Change your query to :
$query = "INSERT INTO visitorsystem.employee(idNumber,name,surname,position,email)
SELECT '$idNumber','$name','$surname',positionid,'$email' FROM visitorsystem.position WHERE position LIKE '%$position%'";
First of all, learn about prepared Statements to prevent SQL injection.
Second you should add all values to the select Statement:
query = "INSERT INTO visitorsystem.employee(idNumber,name,surname,position,email)
SELECT $idNumber,'$name','$surname',positionid,'$email' FROM visitorsystem.position WHERE position LIKE '%$position%'";
Also you do not need singlequotes around $idNumber, because it is numeric
I cant find what happened to the MYSQL query. It was working fine and today it is throwing 1064 error can anyone help me.
In query browser i am getting 1064 error
and in mysqli_error i am getting "Unknown column 's.firstname' in 'field list'".
query ::
SELECT s.recid userId, s.firstname firstName, s.lastname lastName, s.email email, s.status status
FROM usertable s
WHERE s.email = 'xyz#gmail.com'
AND s.mobile = 'XXXXXXXX'
AND (s.enddate IS NULL OR s.enddate = '0000-00-00' OR s.enddate > '2015-04-08 11:13:34')
Error :
Unknown column 's.firstname' in 'field list'
Check the firstname spelling, white spaces on start and end of the field, if its okay, then remove firstname from query and then try it once.
This will help us to debug.
Check your table has the column firstname
Use below command to check it within query browser
DESC usertable;
Try to wrap everything with backticks (or at least column name called status) and see what happens...
SELECT `s`.`recid` `userId`, `s`.`firstname` `firstName`, `s`.`lastname` `lastName`, `s`.`email` `email`, `s`.`status` `status`
FROM `usertable` `s`
WHERE `s`.`email` = 'xyz#gmail.com'
AND `s`.`mobile` = 'XXXXXXXX'
AND (`s`.`enddate` IS NULL OR `s`.`enddate` = '0000-00-00' OR `s`.`enddate` > '2015-04-08 11:13:34')
Note that you are using status as column name, but even if it isn't declared as keyword it is used in queries like SHOW STATUS...So I will avoid that and change its name to something else. Also using status like this in some versions of phpMyAdmin produced some unexpected results. But when you wrap it with backticks you can use it as column name.
I'm performing the following query and it seems I have an issue right where I'm doing the convert(aes_decrypt) part as the query was working fine before I added it in. I know it's something small but can't quite seem to get the statement correct.
Missing quotes or wrong quotes? I just can't see it..
$db->query("SELECT
`users`.`name` AS `users`.`name` ,
convert(aes_decrypt(`users`.`email`,".$salt.") using utf8 AS `users`.`email` ,
`users`.`address` AS `users`.`address`
FROM `users`");
The exact error I'm receiving is:
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 '.name ,convert(aes_decrypt(users.email,MySecretSa' at line
1
Alias names are user defined. Qualifiers on them is meaning less and not supported.
select users.column_name as users.column_name
from users -- this syntax is not correct
If you want just the same name as column name, omit alias name.
select column_name from table_name
And if you want explicit column/expression alias names don't use table qualifiers.
select column_name as alias_name
, col1 * col2 as expression_alias
from table_name
Yes ,it's strange: users.name AS users.name
The error in the second users.name is wrong.
can make it like this
$db->query("SELECT
`users`.`name` AS `user_name` ,
convert(aes_decrypt(`users`.`email`,".$salt.") using utf8 AS `users`.`email` ,
`users`.`address` AS `users`.`address`
FROM `users`");
Or you can eliminate like this
$db->query("SELECT
`users`.`name`,
convert(aes_decrypt(`users`.`email`,".$salt.") using utf8 AS `users`.`email` ,
`users`.`address` AS `users`.`address`
FROM `users`");
Try this sql query:-
$db->query("SELECT
`users`.`name` AS name ,
CONVERT( aes_decrypt(`users`.`email`, '$salt') USING utf8) AS email ,
`users`.`address` AS address
FROM `users`");
Hello this is driving me crazy. I can't get this query to work with the WHERE clause. It works without it. I have tried everything. I have looked at dozens of websites and dozens of questions here. I can't see anything wrong with this query. But it gives me this error whenever I try to use a WHERE clause:
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 `cond` = '1'' at line 4
I have tried it with spaces and without, with single quotes and without, I have tried mysqli. I just can't figure out what the problem is.
Here is the code I currently have:
$sql = <<<SQL
SELECT *
FROM `master_inv`
ORDER BY `sku`
WHERE `cond` = '1'
SQL;
WHERE clause goes before ORDER BY.
$sql = <<<SQL
SELECT *
FROM `master_inv`
WHERE `cond` = '1'
ORDER BY `sku`
SQL;
Your SQL statement is out of order and your missing a select statement.
The ORDER BY statement should be placed after the WHERE clause
SELECT *
FROM master_inv
WHERE cond = '1'
ORDER BY sku
It is just the order which is incorrect try:
SELECT *
FROM `master_inv`
WHERE `cond` = '1'
ORDER BY `sku`
$threadID=$_GET['threadID'];
$result=mysql_query("
SELECT * FROM threads AS Threads
INNER JOIN users AS Users ON Threads.user_id=Users.user_id
WHERE thread_id='$threadID' LIMIT 1
") or die(mysql_error());
I get this:
u have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near ''11' at
line 3
I wrote many inner joins before. Why is my syntax wrong near the end of the query
updated: 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 '12' LIMIT 1' at line 3
First of all, your query is WIDE open to sql injection attacks.
Second, to figure out why you get the syntax error, make the query building phase separate from the actual query call:
$sql = "SELECT ....";
echo $sql;
$result = mysql_query($sql) or die(...);
This way you can see the entire query. MySQL's error messages only report the portion of the query from where it thinks the error is onwards, but sometimes it decides wrong and elminates the actual relevant part where th error is... so... examine the ENTIRE query.
ah, may be I know the answer.
There is a thing in mysql, i believe, called "strict mode" or something, which being too picky about data types.
Try to make your query like this
$id = (int)$_GET['threadID'];
$sql = "SELECT * FROM threads t INNER JOIN users u ON t.user_id=u.user_id
WHERE thread_id=$id LIMIT 1";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
Why not join your query in this fashion:
$threadID = mysql_real_escape_string( $_REQUEST['threadID'] ); // Get some cleanup in there...
$result=mysql_query("SELECT threads.*, users.*
FROM threads, users
WHERE threads.user_id = users.user_id
AND threads.thread_id = '$threadID'
LIMIT 1
") or die( mysql_error() );