以达到不同的输出。我想使用准备好的语句,以便选择查询将根据标签的值输出不同的记录。因为我刚刚切换到PDO,我不确定如何做到这一点。
我看过一些教程,比如http://php.net/manual/en/pdo.prepared-statements.php,但是它显示的是准备好的语句的固定值,而不是我可以通过输入不同的URL来改变的值。
当前代码:<?php
$pdo=new PDO("mysql:dbname=createyo_TestDatabase;host=localhost","createyo_james","password");
$statement=$pdo->prepare("SELECT * FROM Articles where Tags = ?");
$statement->execute(array($tag));
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
print $json;
?>
您只需要添加占位符并将值数组传递给execute
$statement=$pdo->prepare("SELECT * FROM Articles where Tags = ?");
$statement->execute(array($tag));