为什么PHP将MySQL INT列视为字符串? [英] Why PHP considers MySQL INT columns as strings?
问题描述
我正在使用Laravel查询构建器来搜索类别。
这里是:
I am using a Laravel query builder to search for categories. Here it is:
$array[] = $categories->where('pk_i_id', $category_id)->first();
我必须手动将类别ID转换为字符串,即使在数据库中它是Integer类型。为什么要这样做?
I have to manually convert the category id to string, even though in database it is Integer type. Why do I have to do this?
其实 - 我必须在安装了灯泡堆叠的Linux机器上执行此操作。在具有Xampp的Windows机器上,它将该列视为整数。
Actually - I have to do this on Linux machine with Lamp stack installed. On Windows machine with Xampp it considers the column as integer as it should.
推荐答案
该问题听起来更像是 $ categories
已经是 Collection
,而不是 Builder
。 Collection
和 Builder
有一个,其中()
方法,但它们的逻辑不一样。
The issue sounds more like that $categories
is already a Collection
, not a Builder
. Both the Collection
and the Builder
have a where()
method, but their logic is not the same.
where()
方法 Builder
将在数据库的查询运行中添加一个参数化的where子句。在这种情况下,变量的类型并不重要。
The where()
method on the Builder
will add a parameterized where clause to the query run against the database. In this case, the type of variable doesn't matter.
但是,在$($)code 集合
将循环遍历集合,并返回第一个参数中的字段严格相等的结果( ===
)到第二个参数中传递的值。要改变这个,你可以通过 false
作为第三个参数,它将使用一个松散的比较( ==
)而不是严格。此外,您可以使用 whereLoose()
,这是其中()
的快捷方式,第三个参数为 false
。
However, the where()
method on the Collection
will loop through the collection and return those results where the field in the first parameter is strictly equal (===
) to the value passed in the second parameter. To change this, you can pass false
as the third parameter, and it will use a loose comparison (==
) instead of strict. Additionally, you could use whereLoose()
, which is a shortcut for where()
with the third parameter as false
.
$array[] = $categories->where('pk_i_id', $category_id, false)->first();
// or
$array[] = $categories->whereLoose('pk_i_id', $category_id)->first();
如果不正确的字段类型导致更多的问题你所描述的,那么你可能想要解决基本问题。在链接的帖子中已经指出,在LAMP堆栈中,您需要用 mysqlnd
替换 mysqld
司机。
这篇关于为什么PHP将MySQL INT列视为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!