用准备好的陈述节省时间

时间:2014-03-07 15:58:54

标签: mysql prepared-statement

下面我正在搜索五个不同的视频并接收每个视频的类型。有没有办法可以将所有五个组合成一个查询,以使我的代码更有效?我确信有一种方法,只是我正在努力学习语法。

$stmt = $mysqli->prepare("SELECT Genre FROM videos Where VideoId = ?"); 
        $stmt->bind_param('s',$item1);// Video one
        $stmt->execute();
        $stmt->bind_result($Genre1);
        $stmt->fetch();
        $stmt->close();

        $stmt = $mysqli->prepare("SELECT Genre FROM videos Where VideoId = ? ");
        $stmt->bind_param('s',$item2); // video two
        $stmt->execute();
        $stmt->bind_result($Genre2);
        $stmt->fetch();
        $stmt->close();

        $stmt = $mysqli->prepare("SELECT Genre FROM videos Where VideoId = ? ");
        $stmt->bind_param('s',$item3);
        $stmt->execute();
        $stmt->bind_result($Genre3);
        $stmt->fetch();
        $stmt->close();

        $stmt = $mysqli->prepare("SELECT Genre FROM videos Where VideoId = ? ");
        $stmt->bind_param('s',$item4);
        $stmt->execute();
        $stmt->bind_result($Genre4);
        $stmt->fetch();
        $stmt->close();

        $stmt = $mysqli->prepare("SELECT Genre FROM videos Where VideoId = ? ");
        $stmt->bind_param('s',$item5);
        $stmt->execute();
        $stmt->bind_result($Genre5);
        $stmt->fetch();
        $stmt->close();

1 个答案:

答案 0 :(得分:0)

 $Genres = array();
 $items = array($item1, $item2, $item3,$item4,$item5);
 $stmt = $mysqli->prepare("SELECT Genre FROM videos Where VideoId = ? ");

 foreach ($items as $index => $item) {

    $stmt->bind_param('s',$item);
    $stmt->execute();
    $stmt->bind_result($Genres[$index]);
    $stmt->fetch(); 
 } 

 $stmt->close();