搜索

首页  >  问答  >  正文

SQLSTATE: 无效的参数数量:绑定变量的数量与令牌的数量不匹配,位于第102行

<p>我收到了SQLSTATE[HY093]错误:绑定变量的数量与令牌的数量不匹配,位于comments.php的第102行以下:</p> <pre class="brush:php;toolbar:false;"><?php /*** 用于处理文章的类*/ class Comment { // 属性 /*** @var int 数据库中的文章ID*/ public $id = null; /*** @var int 文章将要/已经发布的日期*/ public $publicationDate = null; /*** @var string 文章的完整标题*/ public $title = null; /*** @var string 文章的HTML内容*/ public $content = null; /*** @var int 数据库中的文章ID*/ public $articleid = null; /*** 使用提供的数组中的值设置对象的属性 * * @param assoc 属性值*/ public function __construct( $data=array() ) { if ( isset( $data['id'] ) ) $this->id = (int) $data['id']; if ( isset( $data['publicationDate'] ) ) $this->publicationDate = (int) $data['publicationDate']; if ( isset( $data['title'] ) ) $this->title = preg_replace ( "/[^.,-_'"@?!:$ a-zA-Z0-9()]/", "", $data['title'] ); if ( isset( $data['content'] ) ) $this->content = $data['content']; if ( isset( $data['articleid'] ) ) $this->articleid = (int) $data['articleid']; } /*** 使用编辑表单的POST值设置对象的属性 * * @param assoc 表单POST值*/ public function storeFormValues( $params ) { // 存储所有参数 $this->__construct( $params ); // 解析并存储发布日期 if ( isset($params['publicationDate']) ) { $publicationDate = explode ( '-', $params['publicationDate'] ); if ( count($publicationDate) == 3 ) { list ( $y, $m, $d ) = $publicationDate; $this->publicationDate = mktime ( 0, 0, 0, $m, $d, $y ); } } } public static function getById( $id ) { $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "SELECT *, UNIX_TIMESTAMP(publicationDate) AS publicationDate FROM comments WHERE id = :id"; $st = $conn->prepare( $sql ); $st->bindValue( ":id", $id, PDO::PARAM_INT ); $st->execute(); $row = $st->fetch(); $conn = null; if ( $row ) return new Comment( $row ); }/*** 返回数据库中的所有(或一定范围内的)文章对象 * * @param int 可选 要返回的行数(默认为全部) * @param string 可选 按照哪个列对文章进行排序(默认为"publicationDate DESC") * @return Array|false 一个包含两个元素的数组:results => array,文章对象的列表;totalRows => 文章的总数*/ 公共静态函数 getList( $art=1, $order="publicationDate DESC", $numRows=10000 ) { $conn = 新 PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate) AS PublicationDate FROM comments WHERE Articleid = :art ORDER BY " . mysql_escape_string($order) . " LIMIT :numRows"; $st = $conn->准备( $sql ); $st->bindValue( ":art", $art, PDO::PARAM_INT ); $st->execute(); $列表=数组(); while ( $row = $st->fetch() ) { $comments = 新评论( $row ); $列表[] = $评论; } } /*** 将当前的文章对象插入到数据库中,并设置其ID属性。*/ 公共函数插入(){ // 插入文章 $conn = 新 PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "INSERT INTO comments (publicationDate, title, content,articledid) VALUES (FROM_UNIXTIME(:publicationDate), :title, :content, :articleid)"; $st = $conn->准备 ( $sql ); $st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT ); $st->bindValue( ":title", $this->title, PDO::PARAM_STR ); $st->bindValue( ":content", $this->content, PDO::PARAM_STR ); $st->bindValue( ":articleid", $this->articleid, PDO::PARAM_STR ); $st->execute(); $this->id = $conn->lastInsertId(); $conn = null; } /** * 在数据库中更新当前的文章对象。*/ 公共函数更新(){ // 更新文章 $conn = 新 PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "更新评论 SETpublicationDate=FROM_UNIXTIME(:publicationDate), title=:title,summary=:summary,content=:content,articleid=:articleid,imageExtension=:imageExtension WHERE id = :id"; $st = $conn->准备 ( $sql ); $st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT ); $st->bindValue( ":title", $this->title, PDO::PARAM_STR ); $st->bindValue( ":content", $this->content, PDO::PARAM_STR ); $st->bindValue( ":articleid", $this->articleid, PDO::PARAM_STR ); $st->bindValue( ":id", $this->id, PDO::PARAM_INT ); $st->execute(); $conn = null; } /*** 从数据库中删除当前的文章对象。*/ 公共函数删除(){ // 删除文章 $conn = 新 PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $st = $conn->prepare("DELETE FROM comments WHERE id = :id LIMIT 1"); $st->bindValue( ":id", $this->id, PDO::PARAM_INT ); $st->execute(); $conn = null; } } ?></pre> <p><br />></p>
P粉386318086P粉386318086453 天前463

全部回复(1)我来回复

  • P粉724256860

    P粉7242568602023-08-18 11:09:56

    您没有在此处绑定所有的绑定

    $sql = "UNIX_TIMESTAMP(publicationDate) AS publicationDate
            FROM comments WHERE articleid = :art 
            ORDER BY some LIMIT :numRows";
    
    $st = $conn->prepare( $sql );
    $st->bindValue( ":art", $art, PDO::PARAM_INT );

    您声明了一个名为:numRows的绑定,但您实际上没有将任何内容绑定到它上面。

    回复
    0
  • 取消回复