$colName = rent_due_date
我正在尝试使用$colName
作为我的列名,并使用该列名运行SELECT
语句。
如何在准备好的$colName
语句中使用SELECT
并回复$row[$colName]
?
<?php
$colName = $_GET['colName'];
$smt = $pdo->prepare('SELECT * FROM dhr WHERE ? < curdate()');
$smt = $pdo->bindParam(1,$colName);
if($smt){
if($smt->execute()){
while($row = $smt->fetch()){
?>
// echo in the table as below
<td class="center "><?php echo $row[$colName];?></td>
错误
调用未定义的方法PDO :: bindParam()
答案 0 :(得分:1)
statement
对象上的参数而不是PDO
连接。
这就是你得到Fatal error: Call to undefined method PDO::bindParam()
所以改变:
$colName = $_GET['colName'];
$query = "SELECT * FROM dhr WHERE $colName < NOW()";
echo $query;
$smt = $pdo->prepare($query);
if($smt){
echo "prepare() sucess\n";
$result = $smt->execute();
if($result){
echo "execute() sucess\n";
$data = $smt->fetchAll(PDO::FETCH_ASSOC);
var_dump($data);
//display table
}else{
echo "execute() failed\n";
}
}else{
echo "prepare() failed\n";
}