php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #81576 PDO::MYSQL_ATTR_INIT_COMMAND has int typehint, should be string
Submitted: 2021-10-31 06:09 UTC Modified: 2021-11-06 08:43 UTC
Votes:10
Avg. Score:4.3 ± 0.8
Reproduced:10 of 10 (100.0%)
Same Version:7 (70.0%)
Same OS:6 (60.0%)
From: matthias dot wirtz at hotmail dot de Assigned:
Status: Verified Package: PDO MySQL
PHP Version: 8.1.0RC5 OS:
Private report: No CVE-ID: None
 [2021-10-31 06:09 UTC] matthias dot wirtz at hotmail dot de
Description:
------------
https://www.php.net/manual/de/ref.pdo-mysql.php

PDO::MYSQL_ATTR_INIT_COMMAND (int) has a int typehint giving me this error in PHP 8.1:

 Got error 'PHP message: PHP Fatal error:  Uncaught TypeError: Attribute value must be of type int for selected attribute, string given

Shouldn't the command be a string?

Test script:
---------------
new PDO(DB_CONN, DB_USER, DB_PASS, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4']);


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-03 10:09 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem -Assigned To: +Assigned To: cmb
 [2021-11-03 10:09 UTC] cmb@php.net
Oh, right!  Thanks for reporting.
 [2021-11-03 10:51 UTC] cmb@php.net
-Status: Verified +Status: Feedback -Type: Documentation Problem +Type: Bug
 [2021-11-03 10:51 UTC] cmb@php.net
The docs are fixed[1], but I cannot reproduce the reported error,
neither with strict_types nor when setting the attribute via
::setAttribute().  And the implementation doesn't look like this
error could be thrown.  What am I missing?

[1] <https://github.com/php/doc-en/commit/b2379902aa98b534a21ad904b82c3f568c9efa32>
 [2021-11-06 07:54 UTC] matthias dot wirtz at hotmail dot de
thanks looking at it!

from the docs this attr can only be set when constructing the database handle and i:

"Note, this constant can only be used in the driver_options array when constructing a new database handle."

i just noticed that the same code is used to init the connection for mysql and sqlite - so while this comment is for mysql the actual dbh is for sqlite.

this worked in php8.0 but now fails. i guess it's thrown here:

https://github.com/php/php-src/blame/ee2cec186c35b7b392eeee4821b69b11e11560d0/ext/pdo/pdo_dbh.c#L680
 [2021-11-06 08:43 UTC] cmb@php.net
-Status: Feedback +Status: Verified -Type: Bug +Type: Documentation Problem -Assigned To: cmb +Assigned To:
 [2021-11-06 08:43 UTC] cmb@php.net
> i just noticed that the same code is used to init the connection
> for mysql and sqlite - so while this comment is for mysql the
> actual dbh is for sqlite.

Ah, I see!  The values of the driver specific constants overlap, so
setting PDO::MYSQL_ATTR_INIT_COMMAND on an SQLite3 PDO connection is
actually setting PDO::PDO_SQLITE_ATTR_EXTENDED_RESULT_CODES, what
expects an int (could actually be bool, though).  This should probably
be documented better, and the SQLite driver specific constants are not
documented at all, although there are three of them[1].

[1] <https://github.com/php/php-src/blob/php-8.1.0RC5/ext/pdo_sqlite/php_pdo_sqlite_int.h>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 03:01:29 2024 UTC