복붙노트

[SQL] SQLSTATE [HY093] : 유효 파라미터 번호 : 바인드 변수의 개수가 선 (102)에 토큰의 수가 일치하지 않는 [폐쇄]

SQL

SQLSTATE [HY093] : 유효 파라미터 번호 : 바인드 변수의 개수가 선 (102)에 토큰의 수가 일치하지 않는 [폐쇄]

나는 SQLSTATE의 오류가 발생하고 [HY093] : 잘못된 매개 변수 번호 : 바운드 변수의 수는 아래 comments.php에서 라인 (102)에 토큰의 수와 일치하지 않습니다

<?php

/**
 * Class to handle articles
 */

class Comment
{
  // Properties

  /**
  * @var int The article ID from the database
  */
  public $id = null;

  /**
  * @var int When the article is to be / was first published
  */
  public $publicationDate = null;

  /**
  * @var string Full title of the article
  */
  public $title = null;

  /**
  * @var string The HTML content of the article
  */
  public $content = null;

    /**
     * @var int The article ID from the database
     */
    public $articleid = null;


  /**
  * Sets the object's properties using the values in the supplied array
  *
  * @param assoc The property values
  */

  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'];
  }


  /**
  * Sets the object's properties using the edit form post values in the supplied array
  *
  * @param assoc The form post values
  */

  public function storeFormValues( $params ) {

    // Store all the parameters
    $this->__construct( $params );

    // Parse and store the publication date
    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 );
  }


  /**
  * Returns all (or a range of) Article objects in the DB
  *
  * @param int Optional The number of rows to return (default=all)
  * @param string Optional column by which to order the articles (default="publicationDate DESC")
  * @return Array|false A two-element array : results => array, a list of Article objects; totalRows => Total number of articles
  */

    public static function getList( $art=1, $order="publicationDate DESC", $numRows=10000 ) {
    $conn = new 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->prepare( $sql );
    $st->bindValue( ":art", $art, PDO::PARAM_INT );
    $st->execute();
    $list = array();

    while ( $row = $st->fetch() ) {
      $comments = new Comment( $row );
      $list[] = $comment;
    }
  }


  /**
  * Inserts the current Article object into the database, and sets its ID property.
  */

  public function insert() {

    // Insert the Article
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "INSERT INTO comments ( publicationDate, title, content, articledid ) VALUES ( FROM_UNIXTIME(:publicationDate), :title, :content, :articleid )";
    $st = $conn->prepare ( $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;
  }


  /**
  * Updates the current Article object in the database.
  */

  public function update() {

    // Update the Article
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "UPDATE comments SET publicationDate=FROM_UNIXTIME(:publicationDate), title=:title, summary=:summary, content=:content, articleid=:articleid,imageExtension=:imageExtension WHERE id = :id";
    $st = $conn->prepare ( $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;
  }


  /**
  * Deletes the current Article object from the database.
  */

  public function delete() {


    // Delete the Article
    $conn = new 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;
  }

}

?>

해결법

  1. ==============================

    1.당신은하지 않았다 바인딩의 모든 바인딩 여기

    당신은하지 않았다 바인딩의 모든 바인딩 여기

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

    numRows의 그러나 당신은 실제로 아무것도 결합하지 : 당신은 바인딩이라고 선언했습니다.

    UPDATE 2019 :이에 점점 upvotes을 유지하고 그것은 또 다른 제안을 생각 나게

    큰 따옴표를 사용하면 따옴표 문자열에 변수를 사용하는거야 그렇다면, 그것은 CONCAT 연산자를 사용하는 것이 무의미, PHP에서 문자열 보간이다. 플립 측면에서, 작은 따옴표는 단지 ​​그것을 이해, 또는 단지 전체 문자열에 사용할 수있는 문자열의 끝에 하나 개의 변수처럼있어 그렇다면, 문자열 보간되지 않습니다.

    사실, 인터프리터는 변수의 문자열을 구문 분석에 대해 상관하지 않기 때문에 여기에 사용할 수있는 마이크로 연산이있다. 부스트는 거의 unnoticable 작은 규모 완전히 무시할 수있다. 문자열을 다음과 같이 사용하는 경우, 매우 큰 응용 프로그램에서, 특히 좋은 오래된 기존 모노리스는 눈에 띄는 성능 향상이있을 수 있습니다. (및 IMO, 어쨌든 읽기 쉽게)

  2. from https://stackoverflow.com/questions/20585535/sqlstatehy093-invalid-parameter-number-number-of-bound-variables-does-not-ma by cc-by-sa and MIT license