douxiong2001 2013-07-26 13:32
浏览 32
已采纳

准备好具有动态数量标准的陈述

I have a query which looks something like this:

$query = "SELECT icon.web_id, 
image.web_id, image.base64_data, 
theme.title, theme.themepath, 
theme.description, filepath.filepath, 
filepath.filename
FROM theme

INNER JOIN icon ON icon.fk_theme_id = theme.id
INNER JOIN filepath ON icon.fk_filepath_id = filepath.id
INNER JOIN image ON icon.fk_image_id = image.id
LEFT JOIN junction_icon_icontag ON junction_icon_icontag.fk_icon_id = icon.id

WHERE 1
" . $filepaths . " 
" . $themes . "
" . $icontags . "

GROUP BY icon.id";

The variables cointains matching criterions like "AND (filepath = 'foo' OR filepath = 'foo2')".

I now want to convert this into a prepared statement. How do I work with the variable number of criterions? From what I have seen in examples I should replace them with ? but since I don't know how many there are before the query is built, I don't know how many questionmarks I should add. Currently I'm checking all parameters in a huge switch statement to validate them. I was hoping that PDO's would simplify this.

  • 写回答

1条回答 默认 最新

  • ds2321 2013-07-26 13:44
    关注

    create your where part like this:

    where (filepath = :filepath0 or filepath = :filepath1 ....)
    

    your $filepaths will be an array:

    $filepaths = array('foo', 'foo2' ....);
    

    Then loop over $filepaths binding using named param:

    foreach($filepaths as $i => $value)
        $stmt->bindValue(':filepath' . $i, $value);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘