Skip to content

mysqli_set_charset now throws an mysqli_sql_exception when incorrect charset is provided #6142

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

Conversation

kamil-tekiela
Copy link
Member

Previously mysqli didn't throw an exception when report mode was set to exception and a wrong charset was provided.

Additionally if an unsupported charset was provided and the pre-check didn't catch it then there would be a confusing warning "Error executing query". Instead let mysqli throw an exception if it is set to exception mode.

This results in 2 new behaviours.

  • If the charset provided is not available in mysqlnd list then an exception will be thrown with a message "Invalid character set was provided".
  • If the charset is in mysqlnd list but the server does not recognize it then we get an exception: mysqli_sql_exception: Unknown character set: '<charset>'

I was unable to find a suitable test case for this. I am new to writing PHP test cases and I don't know if I should create a new one or change an existing one. I would appreciate some guidance.

@nikic
Copy link
Member

nikic commented Sep 16, 2020

I was unable to find a suitable test case for this. I am new to writing PHP test cases and I don't know if I should create a new one or change an existing one. I would appreciate some guidance.

If there is an existing test for set_charset functionality, it's better to change it.

@kamil-tekiela
Copy link
Member Author

I wasn't sure if I should add a test case for the error from the database. e.g. "Variable 'character_set_client' can't be set to the value of 'ucs2'".

@nikic
Copy link
Member

nikic commented Sep 17, 2020

I wasn't sure if I should add a test case for the error from the database. e.g. "Variable 'character_set_client' can't be set to the value of 'ucs2'".

Probably wouldn't hurt? Or is that error condition not reliable?

It's unclear if the support for ucs2 will be added in the future but at the moment it looks like this is impossible.
An exception should be thrown if you try to set charset to ucs2
$link->set_charset('invalid');
} catch (\mysqli_sql_exception $exception) {
echo $exception->getMessage() . "\n";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it but it is difficult to keep the indentation consistent when half of the file is using tabs and the other half is using 4 spaces. My IDE ignores this differences and the file doesn't follow any formatting rules. If I execute CS-fixer it actually fixes formatting but I believe I should leave the formatting as it is. If you want me to execute CS-fixer when updating test cases I can do that, but then I would probably require the CS config file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to the tabs in the SKIPIF section? I've opened #6161 to fix that.

@php-pulls php-pulls closed this in 74d1699 Sep 18, 2020
@kamil-tekiela kamil-tekiela deleted the mysqli-set_charset-should-throw-an-error branch September 24, 2020 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants