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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?