Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
General
I found a bug on DB class
soseki
October 2018
If I have a query like following then I always get 1.
(SELECT
COUNT(id)
FROM A )
UNION
(SELECT
COUNT(id)
FROM B)
To fix this issue, I need to remove "()".
So, that would be like this.
SELECT
COUNT(id)
FROM A
UNION
SELECT
COUNT(id)
FROM B
Harro
October 2018
What code do you use to construct this query?
soseki
October 2018
Sorry for insufficient information.
I have the following code.
$query = "" <= The query that I wrote.
\DB::query($query)->execute();
Environment:
PHP 7.0.25
Fuel: 1.8
Harro
October 2018
So you're passing a query in a string?
Fuel doesn't do anything to a hardcoded query (like adding brackets for example), so what is exactly the problem?
soseki
October 2018
Yes, I am passing query string to DB class just like that.
The first query should return something like following.
120
100
221
But, it gives me "1" somehow.
So, I was trying to fix this issue and found removing
brackets from my query string like second one dose work.
soseki
October 2018
Well....I give you almost complete query string.
(
SELECT
COUNT(id) AS count
FROM
tableA
WHERE
tel IS NOT NULL
AND tel != ''
AND status = 0
AND target NOT IN (SELECT name FROM targets)
)
UNION (
SELECT
COUNT(id) AS count
FROM
tableA AS A
WHERE
(tel IS NULL OR tel = '')
AND status = 0
AND target NOT IN (SELECT name FROM targets)
)
UNION (
SELECT
COUNT(id) AS count
FROM
tableA
WHERE
tel IS NOT NULL
AND tel != ''
AND status = 1
AND target NOT IN (SELECT name FROM targets)
)
UNION (
SELECT
COUNT(id) AS count
FROM
tableA AS A
WHERE
(tel IS NULL OR tel = '')
AND status = 1
AND target NOT IN (SELECT name FROM targets)
)
Harro
October 2018
Accepted Answer
Doesn't really matter what the string is.
The point remains DB::query() executes whatever string you pass it as-is, it doesn't modify the SQL you pass it,
What is possible, is that it doesn't detect it as a SELECT query, so you may have to pass the type explicitly:
\DB::query($sql, \DB::SELECT)->execute().
Although if the type detection goes wrong, you get a boolean returned, and not a value 1.
What does this output, assuming your self written query is in $query?
$stmt = preg_split('/[\s]+/', ltrim(substr($query, 0, 11), '('), 2);
echo '[',strtoupper(reset($stmt)),']';
soseki
October 2018
I see.
Thanks for your explanation : )
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,088
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
260
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
Harro
October 2018
soseki
October 2018