Skip to content
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

Float incorrectly casted to PDO::PARAM_INT in MySQL, should be: PDO::PARAM_STRING #25818

Closed
stemis opened this issue Sep 28, 2018 · 5 comments
Closed

Comments

@stemis
Copy link
Contributor

stemis commented Sep 28, 2018

  • Laravel Version: 5.5.40
  • PHP Version: 7.1
  • Database Driver & Version: MariaDB 10.0.24 (MySQL 5.6)

Description:

When issuing the following command:
User::where('rating', '>', 2.5)->get()
The query that actually goes to the database is:
WHERE rating > 2
What should go to the database in order for correct behavior:
WHERE rating > '2.5'

This is caused by the MysqlConnection using PDO::PARAM_INT
According to this wiki article by PHP it should use PDO::PARAM_STRING
https://wiki.php.net/rfc/pdo_float_type

The PDO extension does not have a type to represent floating point values.

The current recommended practice is to use PDO::PARAM_STR

The line that causes the issue is:

is_int($value) || is_float($value) ? PDO::PARAM_INT : PDO::PARAM_STR

It should be edited to:

is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR 

@themsaid this is something you have touched before.
If this is intentional, could you maybe explain why floating point values are not converted to strings, as per the wiki article.

@stemis stemis changed the title bind double/float as PDO::PARAM_STRING not as INT Float incorretly casted to PDO::PARAM_INT in MySQL, should be: PDO::PARAM_STRING Sep 28, 2018
@staudenmeir
Copy link
Contributor

See also #23850.

@stemis stemis changed the title Float incorretly casted to PDO::PARAM_INT in MySQL, should be: PDO::PARAM_STRING Float incorrectly casted to PDO::PARAM_INT in MySQL, should be: PDO::PARAM_STRING Sep 28, 2018
@stemis
Copy link
Contributor Author

stemis commented Sep 28, 2018

@staudenmeir Thanks for referencing that. I have a read a couple of more issue topics but am still uncertain about why it is not cast to string.

The one that really matters is: #16063 , which highlights the issues with JSON comparison

However, I think it is safe to say that we should not solve one issue by introducing another.

This should at least be mentioned on the Database/Eloquent documentation page that when using a float in the where query, it should first be casted to string. This should already prevent a lot of people having headaches!

@staudenmeir
Copy link
Contributor

staudenmeir commented Sep 28, 2018

I just tested this on the latest MariaDB 10.3.9 (in Homestead) and it works for me.

@laurencei
Copy link
Contributor

Thanks - I'll close this.

@stemis - feel free to submit a PR to the docs with your idea above.

@ionutantohi
Copy link

ionutantohi commented Oct 23, 2018

@stemis do you have PDO::ATTR_EMULATE_PREPARES = true ?

I noticed that this option causes this behaviour. Please check #23850

mfn added a commit to mfn/laravel-framework that referenced this issue Jul 3, 2019
Laravel will miss-behave in multiple ways with MySQL and PgSQL as documented in various issues and even PRs, because people try to get a fix in but _the_ recommendation right now is to *not* use it.

I figured it might save everyones time if ppl fill this out upfront because it's often takes some forth and back until users mention this.

See:
- laravel#29023
- laravel#23850
- laravel#25818
- laravel#27951
- laravel#28149
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

No branches or pull requests

4 participants