【问题标题】:Laravel DB Builder uses string variable as integer IDLaravel DB Builder 使用字符串变量作为整数 ID
【发布时间】:2019-05-04 16:03:41
【问题描述】:

我的一个控制器将 id 字段设为$id,我在查询中使用它。当我检查查询字符串时,它会将 id 作为字符串值传递,如下所示:

select `id`, `name` from `table` where `id` = '1'

如果我将 intval($id) 传递给查询,它仍然可以按预期工作。

select `id`, `name` from `table` where `id` = 1

我的问题:哪一个是优化查询和最佳实践?谢谢。

【问题讨论】:

    标签: mysql sql laravel query-builder


    【解决方案1】:

    首先:(int) 比 intval() 快​​。
    链接:wiki.phpbb.com/Best_Practices:PHP。

    关于是否引用数字:实际上,除非您运行的是 Google 规模的操作,否则这种转换开销将非常小。 链接:stackoverflow.com/questions/6781976

    【讨论】:

      猜你喜欢
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多