gpt4 book ai didi

php - 使用 php 数组选择 mysql 数据不起作用

转载 作者:行者123 更新时间:2023-11-30 01:37:41 27 4
gpt4 key购买 nike

由于某种原因,此代码无法正常工作,我不知道为什么:

$action = array('id', 'lurl', 'account');
$request = 3;
$stm = $pdo->prepare("SELECT ? FROM (SELECT * FROM urls ORDER BY id DESC LIMIT ?) sub ORDER BY id ASC");
$stm->bindValue(1, implode(',', $action));
$stm->bindValue(2, $request, PDO::PARAM_INT);
$stm->execute();
$data = $stm->fetchAll();

它所做的只是返回以下数组:

Array ( [0] => Array ( [id,lurl,account] => id,lurl,account ) [1] => Array ( [id,lurl,account] => id,lurl,account ) [2] => Array ( [id,lurl,account] => id,lurl,account ) )

但是当我像这样在查询中手动输入数据时:

SELECT id,lurl,account FROM (SELECT * FROM urls ORDER BY id DESC LIMIT 3);

它做了它应该做的事情。有谁知道这是为什么吗?

最佳答案

使用bindValue,您只能绑定(bind)“值”,而不能绑定(bind)引用。要绑定(bind)引用,您需要使用bindParam

更改:

$stm->bindValue(1, implode(',', $action));
$stm->bindValue(2, $request, PDO::PARAM_INT);

致:

$stm->bindParam(1, implode(',', $action));
$stm->bindParam(2, $request, PDO::PARAM_INT);

关于php - 使用 php 数组选择 mysql 数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16627771/

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