I am trying to find Payouts where user_id equals 1 from the skyrim table along with it's relations.
The code I executed is:
$user_payout = Payout::fromTable('skyrim')->where('user_id',1)->with('game','cluster')->first();
dd($user_payout);
It gives me this 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 'where user_id = ? limit 1' at line 1 (SQL: select * where user_id = 1 limit 1)
Any reason why this doesn't work?
Try to fetch the data first then do the eager loading of relations
$model = (new Payout)->setTable('skyrim')->where('user_id', 1)->first();
if($model) {
$model->load('game','cluster');
}
Related
I'm running a query inside laravel 5.4 that looks like this:
->orderBy("
select ifnull(distance, 10) from location_distances as ld where ld.location1 = '".Auth::user()->location."' and ld.location2=(select ifnull(location,'') from users as usl where usl.id=statuses.user.id)", "desc")
Im getting the error:
SQLSTATE[42000]: Syntax error or access violation: 1064 Syntax error next to 'as `ld where ld.location1 = 'New york' and ld.location2=(select ifnull(locatio' in line 1
Appearently I cannot use select as inside a subquery, the problem is, this query works fine in phpmyadmin
Can you please help me out?
Thank you
this is my code:
TableName::db()->updateAll(array('updated' => 'NOW()'), "WHERE userID
= ". (string)$id);
This is the errormessage i get:
CDbCommand failed to execute the SQL statement: 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 'WHERE userID = 1043' at line
1. The SQL statement executed was: UPDATE TableName SET updated=:yp0 WHERE WHERE userID = 1043;. Bound with :yp0='NOW()'
The SQL Update Query will succesfully executed, but i want to fix this error.
Somebody have a hint for me how to fix this error?
Solution:
TableName::model()->updateAll(array('updated' => new CDbExpression('NOW()')), "userID= ". (string)$id);
The SQL Update Query will succesfully. Good luck to you
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
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.
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...