php-src icon indicating copy to clipboard operation
php-src copied to clipboard

PHP 8.1 PDO (Probably a Bug) : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'school_types.type_id' in 'where clause' in

Open Bugistan opened this issue 4 years ago • 10 comments

Description

Solved

Problem solved. I have already tested SQL in MySQL separately. It worked there but not from within PHP. The problem for this was somewhere in PHP, not MySQL.

Solution:

  1. I deleted and reinstalled PHP 8.1.
  2. I have enabled all required plugins for PDO from CPanel.

Probably friends working on PHP's files on the server have corrupted it.

Problem: TEST SQL: (erased 2)

I've checked the column and table names are all correct. All of these codes work with PHP 7.4, as I mentioned in the note below. When I upgraded to PHP 8.1, it generates the error I mentioned. Table names, parameters etc. are all correct.

Strangely enough, I get such an error with PHP 8.1. Is there anyone who can help? I've gone through at least 30 different threads about this here, but none of them solved my problem.

SQL:

SELECT schools.school_name,
       schools.id,
       schools.city_id,
       cities.city_name                   AS city_name,
       districts.district_name            AS district_name,
       Group_concat(school_types.type_id) AS school_types,
       0                                  AS school_total_students,
       0                                  AS school_total_teachers,
       "-"                                AS school_total_plans
FROM   schools
       LEFT JOIN cities
              ON cities.id = schools.city_id
       LEFT JOIN districts
              ON districts.id = schools.district_id
       LEFT JOIN school_types
              ON school_types.school_id = schools.id
WHERE  ( schools.school_name LIKE :school_name )
       AND ( schools.city_id = :city_id )
       AND ( schools.district_id = :district_id )
       AND ( schools.official = :school_official )
       AND ( school_types.type_id = :school_types )
GROUP  BY school_types.school_id
ORDER  BY schools.id DESC
LIMIT  0, 10 

PHP PDO:

// The query above:
 $stmt = $this->db::$db->prepare($query);
 // PDO parameters are also completely in the array.
 /*
     where_values output => 
     Array
    (
     [school_name] => %a%
     [city_id] => 2
     [district_id] => 128
     [school_official] => 1
     [school_types] => 2
    )
 */
 foreach($search['where_values'] as $key => &$value) {
     $stmt->bindParam(':' . $key, $value);
 }
 $stmt->execute();
 $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

Error:

Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'school_types.type_id' in 'where clause' in......

Test for PHP:

try{
    $db = new PDO('mysql:host=localhost;dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS);
}catch(PDOException $e){
    print $e->getMessage();
}


$query = 'SELECT schools.school_name,
           schools.id,
           schools.city_id,
           cities.city_name                   AS city_name,
           districts.district_name            AS district_name,
           Group_concat(school_types.type_id) AS school_types,
           0                                  AS school_total_students,
           0                                  AS school_total_teachers,
           "-"                                AS school_total_plans
    FROM   schools
           LEFT JOIN cities
                  ON cities.id = schools.city_id
           LEFT JOIN districts
                  ON districts.id = schools.district_id
           LEFT JOIN school_types
                  ON school_types.school_id = schools.id
    WHERE  ( schools.school_name LIKE :school_name )
           AND ( schools.city_id = :city_id )
           AND ( schools.district_id = :district_id )
           AND ( schools.official = :school_official )
           AND ( school_types.type_id = :school_types )
    GROUP  BY school_types.school_id
    ORDER  BY schools.id DESC
    LIMIT  0, 10';

$stmt =$db->prepare($query);
$search['where_values']['school_name'] = '%a%';
$search['where_values']['city_id'] = 2;
$search['where_values']['district_id'] = 128;
$search['where_values']['school_official'] = 1;
$search['where_values']['school_types'] = 2;

foreach($search['where_values'] as $key => &$value) {
    $stmt->bindParam(':' . $key, $value);
}
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

NOTE: PDO query that works with PHP 7.4 does not work in PHP 8.1.

PHP Version

8.1.4

Operating System

Redhat

Bugistan avatar Apr 13 '22 02:04 Bugistan

This is highly unlikely to be a bug in PHP. What else changed during your upgrade from 7.4 to 8.1?

damianwadley avatar Apr 13 '22 04:04 damianwadley

This is highly unlikely to be a bug in PHP. What else changed during your upgrade from 7.4 to 8.1?

Only PHP has changed. The change was made through the panel, CPanel and it.

Bugistan avatar Apr 13 '22 13:04 Bugistan

I can't reproduce this issue. It's highly unlikely that it would be caused by PHP.

The only reason I can think of this is that you always had this error, but in PHP 7.4 you never enabled PDO error reporting. How did you verify that this code works in PHP 7.4? What about PHP 8.0?

kamil-tekiela avatar Apr 13 '22 13:04 kamil-tekiela

I can't reproduce this issue. It's highly unlikely that it would be caused by PHP.

The only reason I can think of this is that you always had this error, but in PHP 7.4 you never enabled PDO error reporting. How did you verify that this code works in PHP 7.4? What about PHP 8.0?

PHP 8.0 also gives the same error. If PHP 7.4, it returns results from MySQL.

I'll edit the code thoroughly so you can reproduce the error. I'm going to get it to work with copy and paste.

Bugistan avatar Apr 13 '22 13:04 Bugistan

try{
    $db = new PDO('mysql:host=localhost;dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS);
}catch(PDOException $e){
    print $e->getMessage();
}

You don't enable error reporting. Please enable PDO error reporting and try again on PHP 7.4.

Also, I would like to point out two more unrelated issues. Don't print error messages to the users. This exposes your database credentials and creates an attack vector. Don't use the deprecated utf8 charset. Consider upgrading to utf8mb4

kamil-tekiela avatar Apr 13 '22 14:04 kamil-tekiela

try{
    $db = new PDO('mysql:host=localhost;dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS);
}catch(PDOException $e){
    print $e->getMessage();
}

You don't enable error reporting. Please enable PDO error reporting and try again on PHP 7.4.

Also, I would like to point out two more unrelated issues. Don't print error messages to the users. This exposes your database credentials and creates an attack vector. Don't use the deprecated utf8 charset. Consider upgrading to utf8mb4

Error display is on right now as I've tested it. But I'm glad you reminded me of the character set, I changed it :)

Yes, when I enabled reporting, it gave the same error (PHP 7.4). But before activating it was giving the rows. For PHP 8.1, it gives the error even though error reporting is not enabled.

Bugistan avatar Apr 13 '22 14:04 Bugistan

PHP 8.0 has enabled PDO error reporting by default. You can still disable it.

I am closing this bug report as it looks like it is not a bug report at all.

kamil-tekiela avatar Apr 13 '22 14:04 kamil-tekiela

PHP 8.0, varsayılan olarak PDO hata raporlamasını etkinleştirmiştir. Yine de devre dışı bırakabilirsiniz.

Bu hata raporunu kapatıyorum çünkü görünüşe göre bu bir hata raporu değil.

Of course yes, it seems that I am all alone with this error :)

Bugistan avatar Apr 13 '22 14:04 Bugistan

Solved

Problem solved. I have already tested SQL in MySQL separately. It worked there but not from within PHP. The problem for this was somewhere in PHP, not MySQL.

Solution:

  1. I deleted and reinstalled PHP 8.1.
  2. I have enabled all required plugins for PDO from CPanel.

Probably friends working on PHP's files on the server have corrupted it.

Bugistan avatar Apr 13 '22 14:04 Bugistan

thanks

damnhacai avatar Dec 14 '22 19:12 damnhacai