您好我正在写一个网站,我想制作我的自定义功能,从db中选择项目,但我无法解决我的问题,有人帮忙吗?
function select($select, $from, $where, $item)
{
global $db;
if ($where != "")
{
$pdoselect = $db->prepare("select :select from :from where :where = :where2");
$pdoselect->bindParam(":select", $select);
$pdoselect->bindParam(":from", $from);
$pdoselect->bindParam(":where", $where);
$pdoselect->bindParam(":where2", $item);
$pdoselect->execute();
foreach ($pdoselect as $return)
{
echo $return[" . $select . "];
}
} else {
$pdoselect = $db->prepare("select :select from :from");
$pdoselect->bindParam(":select", $select);
$pdoselect->bindParam(":from", $from);
$pdoselect->execute();
foreach ($pdoselect as $return)
{
echo $return[" . $select . "];
}
}
}
答案 0 :(得分:2)
您不能对表名和列名使用占位符,您必须对查询的那些部分进行常规字符串替换。您可以在WHERE
子句中使用占位符来表示您要比较的值。
$pdoselect = $db->prepare("select $select from $from where $where = :value");
$pdoselect->bindParam(':value', $item);