SQL query error in codeigniter - php

I tried the code below in phpmyadmin, but received a syntax error:
select * from `reviews_az`
left join `restaurants_az` on `reviews_az`.`restaurant_id` = `restaurants_az`.`id`
where `source` LIKE %YELP% order by `reviews_az`.`id` desc limit 6);
This is the error log:
MySQL said: Documentation
#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 '%YELP% order by `reviews_az`.`id` desc limit 6)' at line 1

You are missing single quote in like.
Correct query:
select * from reviews_az left join restaurants_az on reviews_az.restaurant_id = restaurants_az.id where source LIKE '%YELP%' order by reviews_az.id desc limit 6

Use below query. Missing quotes in LIKE.
Change
LIKE '%YELP%'
Query
select * from `reviews_az`
left join `restaurants_az` on `reviews_az`.`restaurant_id` = `restaurants_az`.`id`
where `source` LIKE '%YELP%' order by `reviews_az`.`id` desc limit 6;

Related

Convert Mysql statement into Laravel eloquent

Since mysql doesn't allow symbol or parameter in views, I want to put my sql statement direct in my controller. But I keep getting an error.
MySQL statement:
SELECT `nokp`, `tarikh_hadir`, #kod_bah := IF(`kod_bah` IS NULL, #kod_bah,`kod_bah`) 'kod_bah'
FROM (SELECT * FROM v_04_harifullkerjawarga_01 ORDER BY `tarikh_hadir`) t1,(SELECT #kod_bah :=0) t2
where nokp = 8201;
I try to convert in laravel as below:
$results = DB::connection('cams')
->table('v_04_harifullkerjawarga_01')
->select(DB::raw("SELECT `nokp`, `tarikh_hadir`, #kod_bah := IF(`kod_bah` IS NULL, #kod_bah,`kod_bah`) 'kod_bah'"))
->select(DB::raw("SELECT * FROM v_04_harifullkerjawarga_01 ORDER BY `tarikh_hadir`) t1"),
DB::raw("(SELECT #kod_bah :=0) t2"))
->where('nokp','=',8201
->get();
But, I get an error. Please someone can help me solve this.
My 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 'SELECT * FROM v_04_harifullkerjawarga_01 ORDER BY `tarikh_hadir`) t1, (SELECT #k' at line 1 (SQL: select SELECT * FROM v_04_harifullkerjawarga_01 ORDER BY `tarikh_hadir`) t1, (SELECT #kod_bah :=0) t2 from `v_04_harifullkerjawarga_01` where `nokp` = 8201)

Getting error while executing SQL query to get result search by keyword using PHP and MySQL

My requirement is when user will type letter inside text box at front end it will auto search from database and give the result accordingly. I have written some query but it gave me the following error.
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 '%P%) ORDER BY member_id DESC LIMIT 0, 30' at line 1
My code is given below.
$searchKey=$_GET['searchKey'];
$keyword = '%'.$searchKey.'%';
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE (:keyword) ORDER BY member_id DESC ");
My first search keyword was p.
You want
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE '$keyword' ORDER BY member_id DESC ");
or also you could do
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE '" . $keyword . "' ORDER BY member_id DESC ");
(:keyword) is not going to pull in the variable for your keyword into the SQL syntax and also (:keyword) is not valid mysql
Another approach,
$sql =mysqli_query($connect,"SELECT * FROM db_restaurant_basic WHERE rest_name LIKE '".$keyword."' ORDER BY member_id DESC ");
It's better to use single quotes inside double quotes in relevant places when executing SQL queries.Sometimes you'll need to put single quotes for table name as well.Like this,
$sql =mysqli_query($connect,"SELECT * FROM `db_restaurant_basic` WHERE rest_name LIKE '".$keyword."' ORDER BY member_id DESC ");

Converting Sql query to CodeIgniter Active Records

I wanted to query only those items that have a highest likes.
This is the sql query I want to convert into CodeIgnitor's Active Records:
SELECT *, SUM(like) as totalLikes
FROM tbl_like
GROUP BY uploadID
ORDER BY totalLikes DESC
LIMIT 2
CodeIgniter:
public function get_cheezyPic(){
$this->db->select('uploadID, SUM(like) as totalLikes');
$this->db->from('tbl_like');
$this->db->group_by('uploadID');
$this->db->order_by('totalLikes DESC');
$this->db->limit(2);
$query= $this->db->get();
return $query->result_array();}
But when I try to run this code, I've got this error
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 'like) as totalLikes FROM (tbl_like) GROUP BY uploadID ORDER BY totalLikes ' at line 1
SELECT `uploadID`, SUM(like) as totalLikes FROM (`tbl_like`) GROUP BY `uploadID` ORDER BY `totalLikes` desc LIMIT 2
What's wrong with this code?
Thanks for the help.
This'll work for you. Please note that you were using reserved keyword so it should be enclosed with backtic ``.
public function get_cheezyPic(){
$this->db->select('uploadID, SUM(`like`) as totalLikes',false);
$this->db->from('tbl_like');
$this->db->group_by('uploadID');
$this->db->order_by('totalLikes DESC');
$this->db->limit(2);
$query= $this->db->get();
return $query->result_array();
}

MySQL error Reatest-n-per-group

Help me!
SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
GROUP BY ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."'
AND ma_forum.not_status='Ativo'
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC
"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 ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='1' ' at line 9"
Your query is in the wrong order; GROUP BY should be after WHERE and before ORDER BY:
SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."'
AND ma_forum.not_status='Ativo'
GROUP BY ma_forum.not_id
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC
I don't know what database you're using, but here's a link to MySQL's SELECT syntax

MySQL Where Clause error

i have the following statement
$result = mysql_query("SELECT * from rests ORDER BY name asc WHERE flag = '1' LIMIT 0 , 20");
am getting the following error
Invalid 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 'WHERE flag = '1' LIMIT 0 , 20' at line 1
am not sure where am going wrong :(
$result = mysql_query("SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20");
You cannot have the ORDER BY clause before WHERE clause.
Refer to the MySQL select syntax for the correct order of various clauses.
Try this:
SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20
Also Ordering in ascending is default, you may drop the asc.
The ORDER BY clause must be after the WHERE clause. That's all it is.
ORDER BY comes after the WHERE and LIMIT at the end.
WHERE goes before ORDER BY.

Categories