Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
I found a bug on DB class
  • 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
  • What code do you use to construct this query?
  • 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

  • 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?
  • 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.
     
  • 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)

  • HarroHarro
    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)),']';

  • I see.
    Thanks for your explanation : )

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion