Doctrine Silex query with bound params fails - php

[EDIT] Removed parametrized table name and altered values of order and minimum rating count to distinguish values [/EDIT]
I have a simple table with following create table
CREATE TABLE ratings_cache (
id INT NOT NULL AUTO_INCREMENT,
movie_id INT NOT NULL,
movie_title VARCHAR(128),
rating DECIMAL(5,4),
rating_count INT DEFAULT 0,
PRIMARY KEY (`id`)
);
Now, I want to query it using Doctrine with some parameters (app is Silex application of course):
$sql = "
SELECT ratings_cache.*
FROM :table_name
WHERE rating_count > :min_rankings
ORDER BY rating DESC, rating_count DESC
LIMIT :limit";
$stmt = $this->app['db']->prepare($sql);
$stmt->bindValue(':min_rankings', $this->minRanks);
$stmt->bindValue('limit', $this->limit);
return $stmt->execute()->fetchAll();
As you can see the query in this form is simple and it runs smoothly when I run it through Mysql console but when I run the code above I get following error:
An exception occurred while executing '
SELECT *
FROM ratings_cache
WHERE rating_count > :min_rankings
ORDER BY rating DESC, rating_count DESC
LIMIT :limit' with params [20, 30]:
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 ''30'' at line 5
I realize the problem might be with something trivial but I am not very used to Doctrine (usualy prefer handling queries on my own) and simply don't know what might be wrong here...

Related

Laravel DB run two queries at once (SELECT/UPDATE)

I have one table which stores some records as JSON. I'm trying to increment the numeric value of a key from JSON column. Below is the query I'm executing in Laravel using DB facade.
DB::connection()
->select("
SELECT CAST(JSON_EXTRACT(consumed, '$.max_users') AS INT) INTO #tmp
FROM subscriptions
WHERE `id` = '2';
UPDATE subscriptions
SET consumed = JSON_SET(consumed, '$.max_users', #tmp+1)
WHERE `id` = '2';
");
It works perfectly ok in PhpMyAdmin however when attempting to execute it from Laravel it throws an exception, that SQL syntax is incorrect (where is it incorrect?).
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 'UPDATE subscriptions SET consumed = JSON_SET(consumed, '$.max_users', ' at line 5 (SQL: SELECT CAST(JSON_EXTRACT(consumed, '$.max_users') AS INT) INTO #tmp FROM subscriptions WHEREid= '2'; UPDATE subscriptions SET consumed = JSON_SET(consumed, '$.max_users', #tmp+1) WHEREid= '2'; )
My suspicion is select() does not allow two different queries to be ran at once. However in this particular case I need them so in order to make use of #tmp variable. I could retrieve the value of first query and pass it to the second one using PHP, but for the sake of argument, how to run two DB selects at once in Laravel? 🤔
You can use a single update query to increment this value:
UPDATE subscriptions
SET consumed = JSON_SET(consumed, '$.max_users', CAST(JSON_EXTRACT(consumed, '$.max_users') AS INT) + 1)
WHERE `id` = '2';

Is there anything wrong with this SQL statement for Sphinx?

Just to give a background, I'm using Sphinx to do searches via PHP/MySQL. This is run through the system we have. Here is the SQL statement in question:
select * from [TABLE_NAME] where match('#keywords "homeschooling"') and status = 3 order by rand() limit 25
I'm getting this error with the said statement:
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 '' at line 1
However, if I use that exact statement and run it by itself, it does work! So I'm not sure what the problem is.
To recap, SQL statement does not work and returns an error when run via the system but it works when run by itself.
Hope someone can help.
Thanks!
Sphinx, or SphinxSE , doesn't use the full text search syntax of MySQL.
After creating a Sphinx engine table:
CREATE TABLE t1
(
id BIGINT UNSIGNED NOT NULL,
weight INTEGER NOT NULL,
query VARCHAR(3072) NOT NULL,
group_id INTEGER,
INDEX(query)
) ENGINE=SPHINX CONNECTION="sphinx://localhost:9312/test";
The query part of the string corresponds to the sphinx syntax for searching:
SELECT * FROM t1 WHERE query='test it;mode=any';

Select all columns but distinct from database table in Laravel

Bellow SQL command runs perfectly :
select * from `product` group by `owner_name` order by `id` asc
When I translate above code in my Laravel project to get the same result :
Product::select('*')
->orderBy('id','asc')->groupBy('owner_name')
->get();
This laravel code returns me error that
SQLSTATE[42000]: Syntax error or access violation: 1055
'db1.product.id' isn't in GROUP BY (SQL: select * from product group
by owner_name order by id asc)
Problem is I have many duplicated records with slight differences on some of their columns. I need to get them by owner_name and only one time .
Edit your applications's database config file config/database.php
In mysql array, set strict => false to disable MySQL's strict mode
You have don't need to do 'select(*)', by default it will select all columns data.
Try this:
Product::orderBy('id','asc')->groupBy('owner_name')
->get();
And if you want to fetch selected column you can do like this:
Product::select(['column_1', 'column_2'])
->orderBy('id','asc')->groupBy('owner_name')
->get();

MySQL 1064 error in CodeIgniter?

I have query
INSERT INTO subscriptions ( client_id, name, group_id, type )
SELECT clients.id, 'Индивидуал', 0, 1 FROM clients WHERE clients.individual=1;
ALTER TABLE clients DROP COLUMN clients.individual;
ALTER TABLE finance_operations ADD COLUMN sub_id INT NOT NULL DEFAULT 0;
which works fine in Mysql Workbench.
But if I use it in codeIgniter code:
$this->db->simple_query($query);
I get 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 'ALTER TABLE clients DROP COLUMN clients.individual; ALTER TABLE'
at line 4
PDO can not run more than one question one time;
This code is right:
$array = [ "query1", "query2", "query3" ];
foreach($array as $query)
$this->db->simple_query($query);

Why is this SQL incorrect (MySQL)?

I have a following query:
"DELETE * FROM Participations WHERE scheduleId IN (SELECT id FROM Schedule WHERE meetingId = :meetingId) AND userId = :userId"
I assign variables using PDO properly.
In response I get:
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 '* FROM Participations WHERE scheduleId IN (SELECT id FROM Schedule WHERE meeting' at line 1}
It is enough that I replace first DELETE with SELECT and query is done properly.
Besides when I test directly in phpMyAdmin the same query with DELETE in it works fine.
I am confused....
Use DELETE FROM (note the lack of *) instead.
The error message clearly indicates that it is a syntax error near '*'.
Remove * from your Delete statement
DELETE FROM Participations WHERE scheduleId IN
(SELECT id FROM Schedule WHERE meetingId = :meetingId) AND userId = :userId

Categories