Skip to content

PDO::getAttribute() behaves differently than document #12032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SakiTakamachi opened this issue Aug 23, 2023 · 7 comments
Closed

PDO::getAttribute() behaves differently than document #12032

SakiTakamachi opened this issue Aug 23, 2023 · 7 comments

Comments

@SakiTakamachi
Copy link
Member

Description

The following code:

<?php

// PDO::ERRMODE_SILENT
$pdo = new PDO('mysql:host=mysql;dbname=test;', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT]); // my env
var_dump($pdo->getAttribute(PDO::ATTR_TIMEOUT));

// PDO::ERRMODE_EXCEPTION
$pdo = new PDO('mysql:host=mysql;dbname=test;', 'root', ''); // my env
var_dump($pdo->getAttribute(PDO::ATTR_TIMEOUT));

Resulted in this output:

Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in /var/www/html/test/test_mysql.php on line 5
bool(false)

Fatal error: Uncaught PDOException: SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in /var/www/html/test/test_mysql.php:9
Stack trace:
#0 /var/www/html/test/test_mysql.php(9): PDO->getAttribute(2)
#1 {main}
  thrown in /var/www/html/test/test_mysql.php on line 9

But I expected this output instead:

bool(null)
bool(null)

https://www.php.net/manual/en/pdo.getattribute.php#refsect1-pdo.getattribute-returnvalues

I suspect that the error/warning and returning false instead of null is probably just outdated documentation.
Even so, it is strange that warning is output with PDO::ERRMODE_SILENT.

I don't know if the documentation and the implementation need to be fixed, and if so, what should be fixed, so I would like your opinion.

PHP Version

PHP 8.1.22

Operating System

No response

@SakiTakamachi
Copy link
Member Author

I got my expectations wrong. The correct is:

NULL
NULL

@SakiTakamachi
Copy link
Member Author

@Girgias
I apologize for the inconvenience. I don't know who to mention regarding PDO...
Since you previously did a review related to PDO, I would like to use a mention.

@Girgias
Copy link
Member

Girgias commented Sep 6, 2023

Considering there are user notes from 5 years ago that detail workarounds to catch exceptions, I suppose it is the intended behaviour, even if it is weird.

I think this is something that could be changed, but might require some discussion on the internals mailing list to gauge what people think is the correct behaviour.

@SakiTakamachi
Copy link
Member Author

@Girgias
Thank you!

A few days ago, I submitted a proposal on the mailing list to deprecate PDO's error mode attribute.

But still no response from anyone...

I'll try replying to myself again.

@SakiTakamachi
Copy link
Member Author

I plan to open an issue on doc, with the policy of modifying the document except for areas related to error modes.

@Girgias
Copy link
Member

Girgias commented Sep 6, 2023

I think it is because your emails landed in most people's spam folder.

@SakiTakamachi
Copy link
Member Author

Oh, sorry about that.

This morning in Japan time, I added SPF settings.

I'll wait and see if it will still be marked as spam in this state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants