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);
Related
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)
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;
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
I am trying to do following.
Select a Hospital with id = hid from table named as hospital.
Add a value "overall_rating" to it and get all the ratings and make avg of it from another table named as hrating
here is my query
$statement = $conn->prepare('SELECT hospital.*,(SELECT AVG(hrating.rating_h) FROM hrating WHERE hid = hospital.hid) as overall_rating WHERE hid=:hid LIMIT 1');
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 MySQL server version for the right syntax to use near 'WHERE hid='44' LIMIT 1' at line 1"}
Where am i being wrong.
It appears you don't have a " FROM hospital " bit in your query?
SELECT hospital.*,(SELECT AVG(hrating.rating_h)
FROM hrating
WHERE hid = hospital.hid) as overall_rating
FROM hospital -- this line seems to be missing ??
WHERE hid=:hid LIMIT 1
Try this:
SELECT hospital.*, temp.overall_rating
FROM hospital
LEFT JOIN (SELECT hid AVG(rating_h) as overall_rating
FROM hrating group by hid
) temp
on hid = hospital.hid
WHERE hospital.hid=:hid LIMIT 1
I'm trying to use the answer given at : How to get ID of the last updated row in MySQL?
I want the ID of the row which was updated. But the following doesn't work giving the error
Fatal error: Call to a member function fetch_assoc() on a non-object.
$query = "
SET #update_id := 0;
UPDATE locations SET owner_player_id='$player_id', id=(SELECT #update_id := id)
WHERE game_id='$game_id' LIMIT 1;
SELECT #update_id;
";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
$location_id = $row['update_id'];
Thanks
Edit:
$mysqli->error gives 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 'UPDATE locations SET owner_player_id='5', id=(SELECT #update_id := id) WHERE gam' at line 1
Your SQL has failed, that is why your subsequent fetch_assoc() method is also failing.
You will want to look into running your query without using the variables (#update_id) as you are intending, and look at using mysqli::$insert_id.
Without knowing what it is you are trying to do exactly, it's hard to be more specific.
Have to use mysqli::multi_query not mysqli::query. Also then have to use mysqli::next_result and mysqli::store_result to get the correct data.
$query = "SET #update_id := 0; UPDATE locations SET owner_player_id='$player_id', id = ( SELECT #update_id := id ) WHERE game_id='$game_id' LIMIT 1; SELECT #update_id;";
$mysqli->multi_query($query) or die($mysqli->error);
$mysqli->next_result();
$mysqli->next_result();
$result = $mysqli->store_result();
$row = $result->fetch_assoc();
$location_id = $row['#update_id'];