参数号无效:绑定变量数与令牌数不匹配'

时间:2014-01-14 19:49:49

标签: php mysql

我有一个填充$row["app_id"]的查询,当我div id="reveal"更改app_id时。我想要做的是在$ revealstmt查询中插入app_id。我一直收到错误。

 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid     
 parameter number: number of bound variables does not match number of tokens

我的代码如下

    <?php   
    $revealid = $row["app_id"];
    while (isset($revealid)){
            $query3 = "";
            $revealstmt = $conn->prepare("SELECT logs.time_entry AS logs_entry, logs.description AS logs_description, substates.name AS reveal_substates_name FROM logs LEFT OUTER JOIN applications ON logs.fk_app_id = applications.pk_app_id LEFT OUTER JOIN substates ON logs.fk_substate_id = substates.pk_substate_id WHERE logs.fk_app_id = <?php echo $revealid ?> and time_entry >= curdate() - INTERVAL DAYOFWEEK(curdate()) + 14 DAY ORDER BY time_entry DESC;");
            $revealstmt->execute(array('query3' => $query3));
 ?>

 <div id="<?php echo $row["app_name"];?>" class='reveal-modal'>
    <div id="reveal"> 
            <h1><?php echo $row["app_name"]; echo $revealid; ?>
            <br />
            </h1>
            <div class="accordion">
                    <?php while ($revealrow = $revealstmt->fetch()){?>
                            <h3><a href=""><?php echo $revealrow["logs_entry"]?> -|- <? 
 php echo $revealrow["reveal_substates_name"]; ?></a></h3>
                            <div><?php echo $revealrow["logs_description"]; ?></div>
                    <?php  }  ?> <!-- Close Tag for $revealrow -->
    <?php } ?> <!-- Close tag for the while(isset) -->

我尝试过使用foreach但不能因为$row["app_id"]不是一个数组,它一次只返回1个数字。

希望我已经提供了所需的所有信息,如果没有,当然会添加我需要的任何信息。再次感谢社区的所有帮助!

3 个答案:

答案 0 :(得分:2)

我相信你的查询prepare语句有错误的语法。试试这个:

$revealstmt = $conn->prepare("SELECT logs.time_entry AS logs_entry, logs.description AS   logs_description, substates.name AS reveal_substates_name FROM logs LEFT OUTER JOIN applications ON logs.fk_app_id = applications.pk_app_id LEFT OUTER JOIN substates ON logs.fk_substate_id = substates.pk_substate_id WHERE logs.fk_app_id = ".$revealid." and time_entry >= curdate() - INTERVAL DAYOFWEEK(curdate()) + 14 DAY ORDER BY time_entry DESC;");

如果你回显它应该转到stdout,而不是你正在准备的字符串/查询。

答案 1 :(得分:1)

您应该在查询中使用连接。此外,您不希望查询时间长。同样地,你不会在一行中编写所有PHP,对于你的MySQL也是如此:它是另一种语言。

$revealstmt = $conn->prepare(
  "SELECT logs.time_entry AS logs_entry, " . 
    "logs.description AS logs_description, " .
    "substates.name AS reveal_substates_name " . 
  "FROM logs LEFT OUTER JOIN applications " .
  "ON logs.fk_app_id = applications.pk_app_id " . 
  "LEFT OUTER JOIN substates ON logs.fk_substate_id = substates.pk_substate_id " .
  "WHERE logs.fk_app_id = " . $revealid . " AND " .
  "time_entry >= curdate() - INTERVAL DAYOFWEEK(curdate()) + 14 DAY " .
  "ORDER BY time_entry DESC;");
$revealstmt->execute();

另一个问题是你试图将query3分配给某些东西。您的查询中没有?占位符,因此无需在execute()中添加任何内容。但是,根据您$revealid的来源,其他工作解决方案可能不安全。你可以通过这样做来保证安全:

$revealstmt = $conn->prepare(
  "SELECT logs.time_entry AS logs_entry, " . 
    "logs.description AS logs_description, " .
    "substates.name AS reveal_substates_name " . 
  "FROM logs LEFT OUTER JOIN applications " .
  "ON logs.fk_app_id = applications.pk_app_id " . 
  "LEFT OUTER JOIN substates ON logs.fk_substate_id = substates.pk_substate_id " .
  "WHERE logs.fk_app_id = ? AND " .
  "time_entry >= curdate() - INTERVAL DAYOFWEEK(curdate()) + 14 DAY " .
  "ORDER BY time_entry DESC;");
$revealstmt->execute(array($revealid));

答案 2 :(得分:0)

$revealstmt->execute(array('query3' => $query3));这是您的问题,您的查询没有绑定值,但您仍在execute statment发送一系列绑定。

试试这个:

$revealstmt = $conn->prepare("SELECT logs.time_entry AS logs_entry, logs.description AS   logs_description, substates.name AS reveal_substates_name FROM logs LEFT OUTER JOIN applications ON logs.fk_app_id = applications.pk_app_id LEFT OUTER JOIN substates ON logs.fk_substate_id = substates.pk_substate_id WHERE logs.fk_app_id = :query3 and time_entry >= curdate() - INTERVAL DAYOFWEEK(curdate()) + 14 DAY ORDER BY time_entry DESC;");

然后通过传递绑定值来执行yout查询:

$revealstmt->execute(array('query3' => $revealid));

或者:

$revealstmt->bindValue(':query3', $revealid);