Convert Mysql statement into Laravel eloquent - php

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)

Related

How to use mysql variables in doctrine

https://stackoverflow.com/a/12772507/1507546
I want to execute this query through doctrine but I'm getting the error below
[Doctrine\DBAL\Driver\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
'#counter := 0' at line 1
Here is my code
$sql = <<<S
SET #counter = 0;
Select sub.orderid,sub.value,(#counter := #counter +1) as counter
FROM
(
select orderid,
round(sum(unitprice * quantity),2) as value
from order_details
group by orderid
) sub
order by 2 desc
limit 10
S;
stmt = $this->_em->getConnection()->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(AbstractQuery::HYDRATE_ARRAY);
Most sql APIs don't allow multiple statements without extra configuration. You'll need to pass them in as separate statements:
$this->_em->getConnection()->exec("SET #counter = 0"); // May need tweaking, I'm not familiar with Doctrine
$sql = <<<S
Select sub.orderid,sub.value,(#counter := #counter +1) as counter
FROM
(
select orderid,
round(sum(unitprice * quantity),2) as value
from order_details
group by orderid
) sub
order by 2 desc
limit 10
S;
stmt = $this->_em->getConnection()->prepare($sql);

SQL query error in codeigniter

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;

SQL query in codeigniter version conflict

I have the Following SQL Query whic runs perfectly in codeigniter version 3
It Shows error in version 2.
$this->db->select('*');
if($cond!=''){
$this->db->where($cond);
}
$this->db->from('(select * from products1 where pr_id in('.implode(",",$condin).') order by pr_id asc) as a ');
$this->db->join(PRICE_TABLE.' as b','a.pr_id=b.pr_id','inner');
$this->db->limit($limit);
$query=$this->db->get();
return $query;
The error is
A Database Error Occurred
Error Number: 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 'order by pr_id asc) as a) INNER JOIN `pr_product_attr_usa_new` as b ON `a`.`pr_i' at line 2
SELECT * FROM ((select * from products1 where pr_id in(752, `6263`, `542`, `2059)` order by pr_id asc) as a) INNER JOIN `pr_product_attr_usa_new` as b ON `a`.`pr_id`=`b`.`pr_id` WHERE `b`.`pr_stock` = 'yes' LIMIT 6
Filename: D:\XAMPP\htdocs\pr_sites\us\chk\system\database\DB_driver.php
Line Number: 331
The issue is related to the code:
in('.implode(",",$condin).')
which output:
(752, `6263`, `542`, `2059)`
instead of:
(752, `6263`, `542`, `2059`)
The ` symbol is outside the ')'.

Unable to get result rows via mysqli despite query working in phpmyadmin

I have the following query:
SET #runtot:=0;
SELECT q1.d, q1.c, (#runtot := #runtot + q1.c) AS rt
FROM
(SELECT STR_TO_DATE(`date_seen`, '%d %b %Y') AS d, COUNT(*) AS c
FROM `table` WHERE `username` = 'myname' GROUP BY d
ORDER BY d) AS q1
and when i run the query against data I get plenty of results.
However when i run a simply mysqli i get an error:
the error is: Call to a member function fetch_object() on a non-object...
the rest is boilerplate
$query_line_chart_result = mysqli_query($conn, $query_linechart_sql);
while($linechart_result_row = $query_line_chart_result->fetch_object()){
//do something
}
EDIT:
new errors appears the query is not working, despite it working in phpmyadmin
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 'SELECT q1.d, q1.c, (#runtot := #runtot + q1.c) AS rt FROM (SELECT ' at line 1

MySql union Error in codeigniter

I have to fetch values from two tables, I have wrote separate query for fetching from both tables. and both queries worked properly. but I need to get this in one result object. so that I have joined queries using UNION operator. but it throws some error. the query is given below
$query1 = "SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id,dev_family.house_name,dev_ib_account_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_account_registration ON dev_ib_account_registration.member_id=dev_members.id
UNION
SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id, dev_family.
house_name,dev_ib_sub_member_registration.account_id FROM (dev_members)
JOIN dev_family ON dev_family.id=dev_members.family_id
JOIN dev_ib_sub_member_registration ON dev_ib_sub_member_registration.member_id=dev_members.id";
$result = $this->db->query($query);
return $result->result();
and the error is:
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 'JOIN dev_family ON dev_family.id=dev_members.family_id JOIN dev_ib_s' at line 8
There is something wired with the usage of brackets in your second query
(dev_members) <---
when i use your query without them in Fiddle query works ,but using with brackets it produces syntax error ,so try them without ()
updated query
SELECT
dev_members.name,
dev_members.id,
dev_members.age,
dev_members.family_id,
dev_family.house_name,
dev_ib_account_registration.account_id
FROM
dev_members
JOIN dev_family
ON dev_family.id = dev_members.family_id
JOIN dev_ib_account_registration
ON dev_ib_account_registration.member_id = dev_members.id
UNION
SELECT
dev_members.name,
dev_members.id,
dev_members.age,
dev_members.family_id,
dev_family.house_name,
dev_ib_sub_member_registration.account_id
FROM
dev_members
JOIN dev_family
ON dev_family.id = dev_members.family_id
JOIN dev_ib_sub_member_registration
ON dev_ib_sub_member_registration.member_id = dev_members.id

Categories