John Davidson

php - Iget syntax error. where my select query is wrong?

0 comments
Message:


I try to search in a followup table with date and wrote for that 3 input, today, yesterday and of x date to y date. I wrote for this search these codes but I get syntax error.



Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'ORDER BY `call_date_sh`DESC) > 0 THEN 1 END DESC, CASE WHEN (...' at line 3 in search_result.php:135



Where am I wrong?


$search_q = $db->prepare("SELECT * FROM `followup` $fields_in_query ORDER BY

CASE WHEN (
SELECT COUNT(*) WHERE DATE_FORMAT(`call_date_sh`,'%Y-%m-%d') > CURDATE() AND DATE_FORMAT(`call_date_sh`,'%Y-%m-%d') != CURDATE() ORDER BY `call_date_sh` DESC LIMIT 0, 1
) > 0 THEN 1 END DESC,

CASE WHEN (
SELECT COUNT(*) WHERE DATE_FORMAT(`call_date_sh`,'%Y-%m-%d') = CURDATE() ORDER BY `call_date_sh` DESC LIMIT 0, 1
) > 0 THEN 1 END DESC,

CASE WHEN (
SELECT COUNT(*) WHERE DATE_FORMAT(`call_date_sh`,'%Y-%m-%d') < CURDATE() ORDER BY `call_date_sh` DESC LIMIT 0, 1
) > 0 THEN 2 END DESC


limit $start, $count");

my table is:


id    call_date_sh           date_t        f_name
1 2021-11-10 18:10:25 1401-02-25 Sorena
2 2022-05-13 11:27:45 1401-06-20 Atoosa
3 2021-12-05 15:12:05 1401-01-28 Radman


My Answer:
It seems like you are missing a space before the DESC keyword in your ORDER BY clause. Make sure to add a space between the column name and the sorting order. Here is the corrected query:

php
SELECT * FROM followup_table WHERE call_date_sh BETWEEN 'x date' AND 'y date' ORDER BY call_date_sh DESC


Replace 'x date' and 'y date' with the actual dates you want to search between.

Rate this post

5 of 5 based on 1106 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us