SQLSTATE: Invalid number of arguments: The number of bind variables does not match the number of tokens, line 102
P粉386318086
2023-08-17 20:03:40
<p>I received the SQLSTATE[HY093] error: The number of bind variables does not match the number of tokens, below line 102 of comments.php: </p>
<pre class="brush:php;toolbar:false;"><?php
/*** Class for processing articles*/
class Comment
{
// Attributes
/*** @var int article ID in database*/
public $id = null;
/*** @var int The date the article will/has been published*/
public $publicationDate = null;
/*** @var string The full title of the article*/
public $title = null;
/*** @var string HTML content of the article*/
public $content = null;
/*** @var int article ID in database*/
public $articleid = null;
/*** Set the object's properties using the values in the provided array
*
* @param assoc attribute value*/
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'];
}
/*** Set the properties of the object using the POST value of the edit form
*
* @param assoc form POST value*/
public function storeFormValues( $params ) {
// store all parameters
$this->__construct( $params );
// Parse and store the release 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 );
}/*** Return all (or a certain range of) article objects in the database
*
* @param int optional Number of rows to return (default is all)
* @param string optional column according to which articles are sorted (default is "publicationDate DESC")
* @return Array|false An array containing two elements: 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;
}
}
/*** Insert the current article object into the database and set its ID attribute.*/
public function insert() {
// 插入文章
$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;
}
/**
* 在数据库中更新当前的文章对象。*/
public function update() {
// 更新文章
$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;
}
/*** Delete the current article object from the database.*/
public function delete() {
// 删除文章
$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;
}
}
?></pre>
<p><br /></p>
You are not binding all bindings here
You declare a binding called :numRows, but you don't actually bind anything to it.