doubengshao8872 2014-07-22 15:46
浏览 58
已采纳

如何向此PDO添加动态预处理语句?

In order to achieve different outputs. I would like to use prepared statements so the Select where query will output different records depending on the value of Tags. Since I have just switched to PDO, I am unsure on how to do this.

I have looked into tutorials such as http://php.net/manual/en/pdo.prepared-statements.php but this shows fixed values to the prepared statments and not values i can change by entering a different URL.

Current code:

<?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;
?>
  • 写回答

1条回答 默认 最新

  • douxian4888 2014-07-22 15:50
    关注

    You just need to add the place holder and pass the array of values to execute

    $statement=$pdo->prepare("SELECT * FROM Articles where Tags = ?");
    $statement->execute(array($tag));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YOLOv5改进后的结构图
  • ¥15 全志v3s怎么设置高速时钟,使用的荔枝派zero开发板,串口2需要921600的波特率
  • ¥15 关于#单片机#的问题:Lora通讯模块hc-14电路图求内部原理图
  • ¥50 esp32 wroom 32e 芯片解锁
  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题