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

In my Laravel code I use DB to access to database. In controller function named showTeacherList I am trying to get all works of every teacher. But Laravel returns this error. When I dump query, copy past it to phpmyadmin and run, it works. My code:
function showTeacherList($login){
$select_result = DB::select('select id, name from kafedra where login = ?', [$login]);
$kafedra_id = $select_result[0]->id;
$teachers = DB::select('select id, name from teacher where kafedra_id = ?', [$kafedra_id]);
$teacher_works = [];
$i = 0;
foreach ($teachers as $teacher) {
$teacher_works[$i] = DB::select('select * from work where teacher_id = ?' [$teacher->id]);
$i++;
}
return view('kafedra/teacher_list', ['login' => $login, 'name' => $select_result[0]->name, 'teachers' => $teachers, 'teacher_works' => $teacher_works]);
}
Please help, thanks

You have a syntax error in your DB::select statement, specifically within your foreach loop. You've missed a comma separating your query and bindings.
Replace:
$teacher_works[$i] = DB::select('select * from work where teacher_id = ?' [$teacher->id]);
With:
$teacher_works[$i] = DB::select('select * from work where teacher_id = ?', [$teacher->id]);

Related

Laravel SQLSTATE[HY093]: Invalid parameter number after change from php 7.4 to 8.0

when I switch my project to php 8.0 I get the following error:
SQLSTATE[HY093]: Invalid parameter number (SQL: SELECT locales.locale, directives.id FROM locales LEFT JOIN directives ON directives.user_id = 18 AND directives.locale = locales.locale GROUP BY locales.locale;)
here is the code:
public function localeStatus($userId, $table)
{
$resultRows = DB::select(
"SELECT locales.locale, $table.id
FROM locales
LEFT JOIN $table
ON $table.user_id = $userId
AND $table.locale = locales.locale
GROUP BY locales.locale;",
array(1)
);
$status = [];
foreach ($resultRows as $row) {
$status[$row->locale] = isset($row->id);
}
return $status;
}
I think there is a problem parsing the query.
When I run the sql query in phpmyadmin the locales come back and the command works without errors.
Can anyone help me what has changed here in php 8?
Thanks nbk!
I changed array(1) to ['userId' => $userId] and $userId in the query to :userId so it works.
public function localeStatus($userId, $table)
{
$resultRows = DB::select(
"SELECT locales.locale, $table.id
FROM locales
LEFT JOIN $table
ON $table.user_id = :userId
AND $table.locale = locales.locale
GROUP BY locales.locale;",
['userId' => $userId]
);
$status = [];
foreach ($resultRows as $row) {
$status[$row->locale] = isset($row->id);
}
return $status;
}

somethings wrong with my mysql query line

I have this Laravel 4.2 Eloquent Query
$m[] = Auth::user()->id;
foreach ($friends as $friend) {
$m[] = $friend->added_user;
}
$news = NewsFeed::whereIN('user_id', $m )->take($limit)->skip($offset)->get();
Actual query:
$news = NewsFeed::whereIN('user_id', array( 'CUS_6367VkCX5i243656TwM3', 'CUS_G530t786S1GVwlcJ3Nw1', 'CUS_xks5oi3dy2C0sa02usD2' ) )->take(10)->skip(5)->get();
Database:
It should return something, Newsfeed Table has datas in it but
result is none, its empty.
What is wrong with my Laravel 4.2 Eloquent Query?
BUT WHEN I DO THIS:
$news = NewsFeed::whereIN('user_id', $m )->get();
it returns a result, without limit and skip.
It seems that $m[] = $friend->added_user; is where you go wrong
dd($friend->added_user)
You will see in your array $m objects of users. However, in your query what's needed is the user_id of added_user.
In this case, you should modify as follows:
$m[] = $friend->added_user()->first()->user_id; //it depends on what your added_user returns, if it returns an array, you should loop over it to get all ids or use ->lists('user_id') to get an array of user_id
Hope it helps
This will do the trick:
$news = DB::select( DB::raw(" SELECT * FROM `news_feed` WHERE `user_id` IN ('CUS_6367VkCX5i243656TwM3', 'CUS_G530t786S1GVwlcJ3Nw1', 'CUS_xks5oi3dy2C0sa02usD2') LIMIT 5, 5 ") );

Select query in Symfony error

I am getting the below error while fetching data from table.
500 | Internal Server Error | Doctrine_Exception
Couldn't find class tasks_comments
$r = Doctrine_Query::create()
->select('u.worked_hours')
->from('tasks_comments u')
->where('u.tasks_id = ?', $arr_values['tasks_id'])
->andwhere('u.id != ?', $arr_values['id']);
$results1 = $r->execute();
But, I didn't get any error for another table as the above format. please check and suggest me.
change this
->where('u.tasks_id = ?', $arr_values['tasks_id'])
to
->andwhere('u.tasks_id = ?', $arr_values['tasks_id'])

join 3 tables in mysql codeigniter

I have 3 tables in my database :-
tbl_roles(role_id,role_name);
tbl_users(id,role_id,username,email,password);
tbl_tickets_replies(id,ticket_id,user_id,role_id,comments)
role_id, id, id are primary keys of corresponding tables.
i need :-
username from tbl_users.
role_name from tbl_roles.
comments from tbl_tickets
where ticket_id from tbl_tickets_replies = $ticket_id coming as a parameter.
My Model Function is :-
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->from('tbl_tickets_replies');
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
$comments = $this->db->get('tbl_tickets_replies');
return $comments;
}
this is showing database error i.e., I am doing wrong query.
I want to ask how can I join three tables to fetch data from 3 different tables
This error is showing :-
A Database Error Occurred
Error Number: 1066
Not unique table/alias: 'tbl_tickets_replies'
SELECT tbl_tickets_replies.comments, tbl_users.username,
tbl_roles.role_name FROM (tbl_tickets_replies,
tbl_tickets_replies) JOIN tbl_users ON tbl_users.id =
tbl_tickets_replies.user_id JOIN tbl_roles ON
tbl_roles.role_id=tbl_tickets_replies.role_id WHERE
tbl_tickets_replies.ticket_id = '6'
Filename: C:\wamp\www\local.helpdesk.com\bonfire\codeigniter\database\DB_driver.php
Line Number: 330`
You are referring to tbl_tickets_replies twice.
Try this:
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,
tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
return $this->db->get('tbl_tickets_replies');
}
For complex queries, i prefer using the plain SQL as follows.
$sql = "SELECT....";
$q = $this->db->query($sql);
Btw, try removing the table name from db->get function
$comments = $this->db->get(); //change this
Join with condition.
$this->db->select('*'); $this->db->from('articles');
$this->db->join('category', 'category.id = articles.id');
$this->db->where(array('category.id' => 10)); $query =
$this->db->get();

try to perform a query with join with Zend (SE)

I have 4 tables on my DB;
merchants, pay_method, spendings ans transaction_type.
They all have an id and a title, except spend (spend have the Foreign keys of the others and other field like user_id, comment, ... ).
I try to have all the informations of all the tables for an unique user_id!
for perform it, I tried a little test that doesn't run :
public function getSpendingsForUser( $userid )
{
$mercTable = Engine_Api::_()->getDbTable('merchants', 'spendings');
$mercName = $mercTable->info('name');
$table = Engine_Api::_()->getDbTable('spendings', 'spendings');
$spendName = $table->info('name');
$select = $table->fetchAll(
$table->select()
->joinLeft($mercName, "$mercName.merchant_id = $spendName.merchant_id", NULL)
->where('user_id = ?', $userid)
->group('spending_id')
);
return $select;
}
An in the view file :
foreach($this->spendings as $s) {
var_dump($s->comment);
}
How can I make the join correctly to have all the info from the user_id ???
Thanks a lot !!!
You should define doesn't run to help us help you. But I can tell you you probably need to ->setIntegrityCheck(false);
$select = $table->fetchAll(
$table->select()
->setIntegrityCheck(false);
->joinLeft($mercName, "$mercName.merchant_id = $spendName.merchant_id", NULL)
->where('user_id = ?', $userid)
->group('spending_id')
);
Before the join.

Categories