开发者社区> 问答> 正文

关于PHP PDO bindValue 问题

<?
$dbms='mysql'; 
$host='localhost';
$dbName='mysql'; 
$user='root';
$pass='root';
$dsn="$dbms:host=$host;dbname=$dbName";

try {
    $db=new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
} catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");
}
?>

<?
    $result=$db->prepare("select * from user");
    $result->bindValue(":host","字符串");
    $result->execute();
    print_r( $result->fetchAll());
?>

bindValue这个是类似 JDBC中preparedstatement 中setparameter吗

结果是没有进行过滤,表中数据全显示了

要在 SQL语句中加 where 条件 吗?

写成 select * from user where host=? 这样是不对的吧

展开
收起
吴孟桥 2016-06-06 18:14:33 1928 0
1 条回答
写回答
取消 提交回答
  • <?
        $result=$db->prepare("select * from user where host=:host");
        $result->bindValue(":host","localhost");
        $result->execute();
        print_r( $result->fetchAll());
    ?>

    写成这样的形式就可以了

    2019-07-17 19:29:12
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PHP安全开发:从白帽角度做安全 立即下载
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载