PHP exception: SQLSTATE[42000]: Syntax error or access violation: 1064 - php

I have a php pdo script where I want to select the record (from a table in a MySQL database) with the highest number in "field5". I also have a few other constrictions, see below:
$stmt=$db->query("SELECT `field1`,`field2` FROM ".$tablename." WHERE
`field3`!=".$variable1." AND `field3`!=".$variable2." AND
`field4`='xx' AND `field5`<".$variable3." ORDER BY DESC `field5` LIMIT
1");
I have pretty much all the code inside a try-statement, and in the catch statement I use
var_dump($ex->getMessage());
to get the exception message from the exception $ex.
Now, when I execute the code I get the following exception 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 'DESC field5
LIMIT 1' at line 3' (length=232)
I'd be grateful for any advice on what might be wrong!

Change
ORDER BY DESC field5
into
ORDER BY field5 DESC

Kindly refer the MySQL manual for the syntax of select query. You have made a simple mistake which is, the query was syntactically wrong. You can only order the column by referencing it initially :
ORDER BY '{Column-Name}'
and then only you can define how it can be ordered either in ASC or DESC.
ORDER BY '{Column-Name}' [ASC|DESC]
So you have to change the query as shown here :
$stmt=$db->query("SELECT `field1`,`field2` FROM ".$tablename." WHERE
`field3`!=".$variable1." AND `field3`!=".$variable2." AND
`field4`='xx' AND `field5`<".$variable3." ORDER BY `field5` DESC
LIMIT 1");

Related

Why it shows me error when preparing a sentence in PHP with MySQLi

I'm creating a sentence prepared in PHP, and I run into a rare syntax error, I do not know if it is breaching any of MySQL or why I show that error
The syntax is as follows, I want to sort by row and by ascending or descending type and limit the results
$query = "SELECT * FROM myTable ORDER BY ? ? LIMIT? ,?"
if($conn->prepare($query)){ .. } // error
The error 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 '? LIMIT ? , ?' at line 1
If you execute said statement in MySQL, it correctly throws the results
Parameters to ORDER BY are not values, and cannot be parametrised. One is a column reference, the other is a keyword.
For example do like this and try.
$query = "SELECT * FROM myTable ORDER BY column_name LIMIT 0,10";

PHP Error In Syntax: "to use near '-3, 3"

I'm following a tutorial but it seems I'm either doing something wrong, or the tutorial is outdated.
The tutorial told me to run this query:
$recordsPerPage = 3;
$fromRecordNum = ($recordsPerPage * $page) - $recordsPerPage;
$query = $engine->runQuery("SELECT * FROM forum_posts ORDER BY id DESC LIMIT $fromRecordNum, $recordsPerPage WHERE topic_id=:topic");
$query->execute(array(':topic'=>$thread['id']));
But this appears to output this 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 MariaDB server
version for the right syntax to use near '-3, 3 WHERE topic_id='20''
at line 1'
I've tried looking up different tuturials about pagination but it seems they all use this technique.
I hope someone knows how to fix this! Thanks a lot!!

Yajra dataTable filtering . Not working

Im trying to search on Datatable , but getting this error:
Exception 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 'AS fullname like ?) or LOWER(drivers.street_no) LIKE ?
or LOWER(drivers.`s' at line 1
and this what the resultant query
'select count(*) as aggregate from (select '1' as `row_count` from `drivers` where (LOWER(`drivers`.`id`) LIKE %fuj% or LOWER(`drivers`.`licence_no`) LIKE %fuj% or (CONCAT(first_name,' ',last_name) AS fullname like %fuj%) or LOWER(`drivers`.`street_no`) LIKE %fuj% or LOWER(`drivers`.`status`) LIKE %fuj%)) count_row_table'.
Any help would be really appriciated ..
Thanks

CakePHP 3 Error: SQLSTATE[42000]: Syntax error or access violation: 1064

I'm getting this error:
Error: 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 'AS `Colleges__*` FROM college_admins CollegeAdmins LEFT JOIN colleges Colleges O' at line 1
Here is the SQL query which is giving this error:
SELECT Colleges.* AS `Colleges__*` FROM college_admins CollegeAdmins LEFT JOIN colleges Colleges ON Colleges.id = (CollegeAdmins.college_id) WHERE CollegeAdmins.user_id = :c0 LIMIT 20 OFFSET 0
I enabled quoteIdentifiers config\app, but it leads to this new error:
Error: 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 'AS `Colleges__*` FROM `college_admins` `CollegeAdmins` LEFT JOIN `colleges` `Col' at line 1
where the query becomes:
SELECT `Colleges`.* AS `Colleges__*` FROM `college_admins` `CollegeAdmins` LEFT JOIN `colleges` `Colleges` ON `Colleges`.`id` = (`CollegeAdmins`.`college_id`) WHERE `CollegeAdmins`.`user_id` = :c0 LIMIT 20 OFFSET 0
I think it's taking the 'Col from Colleges as the keyword 'COL', but I'm not sure. How to fix this?
This is the CakePHP code which is generating the MySQL query:
return $college_admins->find()
->select(['Colleges.*'])
->leftJoinWith('Colleges')
->where(['CollegeAdmins.user_id' => $userId]);
You cannot use Colleges.* in a CakePHP ORM query (CakePHP 3.x). As you've discovered this creates incorrect SQL aliases like Colleges__*. Instead to select all columns of a table you need to pass a table object.
So you'd probably be wanting to do something like:-
->select($college_admins->Colleges)
Assuming Colleges is associated with your CollegeAdmins table.
You cannot alias colleges.*, since this refers to all columns within colleges table and aliases refer to a single column (or table or subquery). You need to list all fields within the colleges table and provide an alias for each of them, such as
select colleges.ig as colleges_id, colleges.field1 as colleges_field1, ...
There is not syntax in sql to provide alias such way. What you may try to do is to access the metadata returned by mysql in php to retrieve the table name for each field.

PDOException error in MySQL database

I am getting the following error message when executing my query
[2/2] DBALException: An exception occurred while executing 'SELECT DISTINCT s0_.id AS id0, s0_. AS 1 FROM shop s0_ WHERE s0_.isLocked = ? ORDER BY s0_.owner_id DESC LIMIT 20 OFFSET 0' with params [0]:
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 'AS 1 FROM shop s0_ WHERE s0_.isLocked = 0 ORDER BY s0_.owner_' at line 1 +
[1/2] PDOException: 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 'AS 1 FROM shop s0_ WHERE s0_.isLocked = 0 ORDER BY s0_.owner_' at line 1
Is this supposed to be a query error or is this something else ? Because it seems that my query is legit.
SELECT DISTINCT s0_.id AS id0, s0_. AS 1
It is supposed to be a query error. You are selecting the unique id's from table s0_ and a field with no name of that same table, but a field can't have no name.
You should add a fieldname, like this:
SELECT DISTINCT s0_.id AS id0, s0_.FIELDNAME AS `1`
SELECT DISTINCT s0_.id AS id0, s0_. AS `1`
wrap 1 in ``
And the missing field name from comments above...

Categories