关闭。 此问题不符合堆栈溢出准则。它当前不接受答案。
想改善这个问题吗? 更新问题,使其成为Stack Overflow
的主题。
6年前关闭。
我收到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;
}
}
?>
您没有在此处绑定所有绑定
$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的绑定,但实际上从未绑定任何东西。
2019年更新:我一直对此持反对态度,这让我想起了另一个建议
双引号是PHP中的字符串插值,因此,如果要在双引号字符串中使用变量,则使用concat运算符毫无意义。另一方面,单引号不是字符串内插,因此,如果在字符串的末尾仅得到一个变量,则可能有意义,或者仅将其用于整个字符串。
实际上,这里有一个微操作,因为解释器不关心解析字符串以获取变量。这种提升几乎是不明显的,并且在小范围内是完全可以忽略的。但是,在非常大的应用程序中,尤其是旧的老式遗留巨石中,如果像这样使用字符串,可能会显着提高性能。(和IMO,无论如何都更容易阅读)
问题内容: 使用Doctrine 2,我想让一些用户成为另一个用户的联系人。该表包含这些用户之间的映射。函数中的查询将返回以下错误: 参数编号无效:绑定变量的数量与令牌的数量不匹配。 但是,据我所知,它设置为“ b”,设置为“ 2”,两者均由该功能分配。 问题答案: 不要在查询文本中用引号将任何参数引起来! 应该
问题内容: 尝试运行以下功能时,出现错误“ SQLSTATE [HY093]:无效的参数号”: 我觉得这很简单,我只是没有抓住。有任何想法吗? 问题答案: 尝试: 和 文档摘录(http://php.net/manual/zh/pdo.prepare.php): 调用PDOStatement :: execute()时,对于要传递给语句的每个值,必须包含一个唯一的参数标记。您不能在准备好的语句中两
问题内容: 上面的代码因以下错误而失败 SQLSTATE [HY093]:参数号无效:参数未定义 虽然什么时候才执行? 这里发生了什么? 问题答案: 您收到的此错误: SQLSTATE [HY093]:参数号无效:参数未定义 是因为&中的元素数不相同或包含1个以上的元素。 如果包含多个元素,则插入操作将失败,因为query()中仅引用了1个列名 如果&不包含相同数量的元素,则由于查询期望x参数,但
我不明白为什么我会得到这样的错误:“prepared Statement中的变量数不匹配参数数”。 我的代码如下所示:
致命错误:C:\xampp\htdocs\otakutangerang_admin\C_action.php:33堆栈跟踪:#0 C:\xampp\htdocs\otakutangerang_admin\C_action.php(33):PDO语句中出现未捕获的异常“PDOException”,并显示消息“SQLSTATE[HY093]:参数编号无效”-
问题内容: 我一直在尝试使用准备好的语句在PHP中编写登录表单,但是每次尝试登录时,都会出现以下错误: mysqli_stmt :: bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配 这是我的代码: 有人可以告诉我为什么会这样吗? 问题答案: 如果这确实是您的代码,则可能是$ _POST [“ name”]或$ _POST [“ password”]是一个数组,因此bin