这是我的 PHP PDO 准备语句,用于选择和显示数据,但它给出了很多错误。
$sql = "SELECT * FROM `info` WHERE tag1 IN ('$tag1','$tag2') $andor tag2 IN ('$tag1','$tag2 ') ORDER BY $sort DESC LIMIT $limit OFFSET $start";
// prepare as you have
$sth = $conn->prepare($sql);
// Bind parameters while executing
$sth->execute(array(
':name'=>$name,
':tag1'=>$tag1,
':tag2'=>$tag2
));
# Fetch the associate array
$result = $sth->fetch(PDO::FETCH_ASSOC);
for($x=0, $n=count($result); $x < $n; $x++){
echo "Name: ".$result[$x]['name']." ";
}
错误:
Notice: Undefined variable: movie_name
Notice: Undefined offset: 0
如果我不使用准备好的语句,一切都会正常工作。
最佳答案
为了绑定(bind)变量,您需要更改查询。使用 :tag1 代替“$tag1”,依此类推。
问候
关于php - 如何使用 PHP PDO 准备语句选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44493695/