Home » Php » mysql – sql_mode works in php but not in an external db management tool

mysql – sql_mode works in php but not in an external db management tool

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m running the following query using PHP and I get the error:

1055: Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column '_______.a.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

query

    $get_artistid = (int)$_POST['artistid'];
    $q = 'SELECT
        w.work_id,
        w.name,
        CONCAT(w.work_id, \'/\', w.seo_url),
        a.name,
        a.photo_basename
    FROM
        work_credit wc
        JOIN recording r ON wc.work_id = r.work_id
        JOIN WORK w ON wc.work_id = w.work_id
        JOIN artist a ON wc.artist_id = a.artist_id
    WHERE
        wc.artist_id = ?
        AND r.is_performer = 1
        AND r.is_video = 0
    GROUP BY
        w.work_id';

    $conn = fn_connect();

    if (!$stmt = $conn->prepare($q)) {
        echo 'Error #' . $conn->errno . ': ' . $conn->error;
    }

    $stmt->bind_param('i', $get_artistid);
    $stmt->execute();
    $stmt->bind_result($workid, $workname, $workhref, $artistname, $artistbasename);
    ...

PHP is right, i’m supposed to have the error. The query should end with

GROUP BY
        w.work_id, a.name, a.photo_basename

But, when i’m running the same query using a database management tool like TablePlus or SQLPro or else, the query executes correctly without the error. The reason why i’m using an external app is to create, edit, update, optimize the query. Having an app that doesn’t pick up the error defeats the purpose.

When I run the following on any external db management app

SELECT @@sql_mode

I get

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

ONLY_FULL_GROUP_BY looks like it’s enable. Still, it doesn’t pick up the error.

Any ideas why all of the external apps (all of them) are doing this? Do I need to configure something.

I have: Apache 4.4.41, PHP 7.3.11, mySQL 5.7.28 using Catalina 10.15.3

How to&Answers: