0

如何将数组与 sql 查询一起使用,然后循环遍历结果?

这是我所拥有的,但我不确定我应该如何在查询中使用内爆变量

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<titleard Log</title>
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>
<script type="text/javascript">
 window.onload = function() { new JsDatePick({ useMode:2, target:"inputField", dateFormat:"%d-%m-%Y" }); new          JsDatePick({ useMode:2, target:"inputField2", dateFormat:"%d-%m-%Y" }); }; 
</script>
</head>
<body>   

 <form action="#" method="post"> 
 <input type="checkbox" name="driver[]" value="Julia">Julia 
 <input type="checkbox" name="driver[]" value="Pete">Pete From 
 <input type="text" name="date1" size="12" id="inputField" /> To 
 <input type="text" name="date2" size="12" id="inputField2" /> 
 <input type="submit" value="Submit" name="submit"/> 
 </form> 

<?php 
if(isset($_POST['submit']) && ($_POST['submit'] == "Submit")) 
{ 
$date1 = $_POST['date1'];
$date2 = $_POST['date2'];
$date1 = date("Ymd", strtotime($date1));
$date2 = date("Ymd", strtotime($date2));
$drivers = "'".implode("','",$_POST['driver'])."'";
}

$mysqli = new mysqli('' );

if($mysqli->connect_error)
echo {
die("$mysqli->connect_errno: $mysqli->connect
_error");
}

$query = "SELECT * FROM wizardlog WHERE driver IN (?) AND date between ? and ? ";

$stmt = $mysqli->stmt_init();
if(!$stmt->prepare($query))
{
print "Failed to prepare statement\n";
}
else
{
              $stmt->bind_param("sss", $drivers, $date1, $date2);
              $stmt->execute();
            $result = $stmt->get_result();
            while ($row = $result->fetch_array(MYSQLI_ASSOC))
        {
            foreach($rows as $row) 
        { 
        $id = $row['id'];
        $driver = $row['driver'];
        $date = date("d/m/y", strtotime($date));
         $time = $row['time'];
          $time = substr($time, 0, 5);
        $fname = $row['fname'];
        $lname = $row['lname'];
        $town = $row['town'];

任何帮助将不胜感激!

4

1 回答 1

0

一个参数只能代替一个单一的标量值。不是值列表。

要参数化列表,您需要的参数数量等于列表中元素的数量。

但是 mysqli 使得使用具有可变数量元素的bind_param()变得非常困难。原因是 bind_param() 需要你通过引用传递参数,所以你不能只传递一个数组;你需要传递一个引用数组。这是皇家皮塔饼。

使用 PDO 会容易得多,因为您可以简单地将一组值传递给PDOStatement::execute()方法。

于 2013-10-07T21:28:53.903 回答