在以下PHP代码中获取错误

时间:2014-07-16 11:09:08

标签: php sql pdo

$db包含与数据库的连接。 我在foreach语句中收到错误。

错误讯息:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid        parameter number' in /home/a5057270/public_html/insert2.php:33 Stack trace: #0 /home/a5057270/public_html/insert2.php(33): unknown() #1 {main} thrown in /home/a5057270/public_html/insert2.php on line

这里是PHP代码:

$date = $_GET['date'];
$time = $_GET['time'];
$mode = $_GET['mode'];
$tfno = $_GET['tfno'];

$params = array(':date' => $date);
$query = "SELECT * FROM cabs WHERE DATE=':date' ";

if ($mode!=='' || $mode!=="") {
  $query .="AND MODE=':mode' ";
  $params[':mode'] = $mode;
}

if ($tfno!=='') {
  $query .="AND TFNO=':tfno' ";
  $params[':tfno'] = $tfno;
}
$query .="ORDER BY TIME";

$req = $db->prepare($query);
$req->execute($params);

//Build Result String
$display_string = "<article class='container box style3'><section><header><h3><u><b>Here Are The Results...!!</u></b></h3></header><div class='table-wrapper'><table class='default'><thead><tr><th>ID</th><th>Name</th><th>Description</th><th>Contact No.</th></tr></thead><tbody>";

// Getting Error in the line Below

foreach ($req as $row) {
   $display_string .="<tr><td>" . $row[IDNO] . "</td><td>" . $row[NAME] . "</td><td><ul><li> Date : " . $row[DATE] . "</li><li> Time : " . $row[TIME] . "</li><li> 

Train/Flight No. " . $row[TFNO] . "</li></ul></td><td>" . $row[CONTACT] . "</td></tr>";


}


$display_string .= "</table>";

1 个答案:

答案 0 :(得分:1)

那是因为你将''放在你的PDO vars周围...... 删除它们,一切都很好。 示例:制作&#39;:模式&#39; =&GT; :没有''的模式,它将起作用。

$params = array(':date' => $date);
$query = "SELECT * FROM cabs WHERE DATE=:date "; //No '' here

if ($mode!=='' || $mode!=="") {
   $query .="AND MODE=:mode "; //No '' here
   $params[':mode'] = $mode;
}

if ($tfno!=='') {
   $query .="AND TFNO=:tfno ";//No '' here
   $params[':tfno'] = $tfno;
}