problem description
typical mobile phone number problem. The database varchar, mobile phone number is indexed, but php passes the parameter int
public function getByPhone ($phone) {
return $this->userFollow
->where("phone", $phone)
->get();
}
$this- > getByPhone (13845678889); / / the native sql printed by the index
is:
string (48) "select * from zp_user_follow
where phone
=?" Array (1) {[0] = > int (15000475201)}
$this- > getByPhone ("13845678889"); / / Walk index
print out the native sql as follows:
string (48) "select * from zp_user_follow
where phone
=?" Array (1) {[0] = > string (11) "15000475201"}
the environmental background of the problems and what methods you have tried
add the type
public function getByPhone ( string $phone before the variable) {
return $this->userFollow
->where("phone", $phone)
->get();
}
this is a mandatory constraint, but what if the programmer doesn"t write it? If you pass an array, it is ["phone" = > 13845678889] in the array; how to solve it?
related codes
laravel is the underlying code of the pdo,pdo binding parameter, which determines the type used by the third parameter
public function bindValues ($statement, $bindings)
{
foreach ($bindings as $key => $value) {
$statement->bindValue(
is_string($key) ? $key : $key + 1, $value,
is_int($value) || is_float($value) ? PDO::PARAM_INT : PDO::PARAM_STR
);
}
}
what result do you expect? What is the error message actually seen?
is there a way to automatically parse the query parameter type and match the database field type when querying without changing the underlying laravel, even if the programmer has made a mistake? if there is no way, we can only throw the problem to the programmer? Ask them to note that when writing code, declare that the type of query parameters should be the same as that of the database? What if they forget? The problem is enlarged a little bit, if the generation environment has a large amount of data, do not take the index, full table scan, if because of this problem, lead to slow sql, generation environment out of bug, deduction performance? Criticism?