gpt4 book ai didi

php - Laravel Raw SQL 查询命名参数绑定(bind)顺序

转载 作者:可可西里 更新时间:2023-11-01 08:39:01 24 4
gpt4 key购买 nike

“order by”上的命名参数绑定(bind)不适用于此完整的原始语句。不显示错误消息。开始和长度工作。

    $sql = "SELECT
product.id AS 'product-id',
product.name AS 'product-name',
product.status AS 'product-status',
product.ingredients 'product-ingredients',
product.price AS 'product-price',
category.name AS 'category-name'
FROM
product
LEFT JOIN
category ON product.category_id = category.id
ORDER BY :orderBy
LIMIT :start,:length";

return DB::select($sql, [
'orderBy' => $orderBy,
'start' => $start,
'length' => $length
]);

有什么想法吗?

最佳答案

问题出在底层 PDO 语句中。您不能像绑定(bind)值那样在查询中绑定(bind)表名或列名。看到这个答案:

Can PHP PDO Statements accept the table or column name as parameter?

您可以在没有原始表达式的情况下重写您的查询:

return DB::table('product')
->select([
product.id AS 'product-id',
...
])->leftJoin('category', 'product.category_id', '=', 'category.id')
->orderBy($orderBy)
->limit($start, $length)

如果您必须使用原始表达式,则必须按值手动清理订单并将其作为字符串插入到查询中。

关于php - Laravel Raw SQL 查询命名参数绑定(bind)顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47614131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com