GVKun编程网logo

php – SQLSTATE [HY093]:参数号无效:绑定变量数与第102行的标记数不匹配(什么叫sql参数绑定)

17

在本文中,我们将详细介绍php–SQLSTATE[HY093]:参数号无效:绑定变量数与第102行的标记数不匹配的各个方面,并为您提供关于什么叫sql参数绑定的相关解答,同时,我们也将为您带来关于ja

在本文中,我们将详细介绍php – SQLSTATE [HY093]:参数号无效:绑定变量数与第102行的标记数不匹配的各个方面,并为您提供关于什么叫sql参数绑定的相关解答,同时,我们也将为您带来关于java.sql.SQLException:列数与第1行错误处的值计数不匹配、MySQL和PHP错误:列数与第1行的值数不匹配、Mysql和PHP错误:列计数与第1行的值计数不匹配、PHP PDOException:“ SQLSTATE [HY093]:无效的参数编号”的有用知识。

本文目录一览:

php – SQLSTATE [HY093]:参数号无效:绑定变量数与第102行的标记数不匹配(什么叫sql参数绑定)

php – SQLSTATE [HY093]:参数号无效:绑定变量数与第102行的标记数不匹配(什么叫sql参数绑定)

我收到错误的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,$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_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_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_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",PDO::ParaM_STR );
      $st->bindValue( ":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_PASSWORD );
    $st = $conn->prepare ( "DELETE FROM comments WHERE id = :id LIMIT 1" );
    $st->bindValue( ":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",PDO::ParaM_INT );

你已经声明了一个名为:numRows的绑定,但你实际上从未绑定任何东西.

java.sql.SQLException:列数与第1行错误处的值计数不匹配

java.sql.SQLException:列数与第1行错误处的值计数不匹配

我正在尝试在表中插入数据,但是显示以下错误:

java.sql.SQLException:列数与第1行的值数不匹配

我已经搜索了此错误,并尝试了所有解决方案,但仍然无法正常工作。这是我的代码:

class.html

<html>    <head>        <title>Class</title>        <meta charset="UTF-8">        <meta name="viewport" content="width=device-width, initial-scale=1.0">    </head>    <body>         <form method="post" action="class.jsp">            <center>                <table border="1" width="30%" cellpadding="5">                    <thead>                    <tr>                        <th colspan="2">Enter Information Here</th>                    </tr>                </thead>                <tbody>            <tr>                        <td>Class Name</td>                        <td><input type="text" name="name" value="" /></td>                    </tr>                    <tr>                        <td>Class Strength</td>                        <td><input type="text" name="strength" value="" /></td>                    </tr>                    <tr>                        <td>Room</td>                        <td>                           <input type="text" name="room" value="">                        </td>                    </tr>                    <tr>                        <td>Section</td>                        <td><input type="text" name="section" value="" /></td>                    </tr>                    <tr>                        <td><input type="submit" value="Submit" /></td>                        <td><input type="reset" value="Reset" /></td>                    </tr>            </tbody>                </table>            </center>        </form>    </body></html>

class.jsp

<%@ page import ="java.sql.*"  import= "java.sql.Connection"%><%    String cname = request.getParameter("name");    String cstrength = request.getParameter("strength");    String croom = request.getParameter("room");    String csection = request.getParameter("section");    //String available = request.getParameter("bavailable");    Class.forName("com.mysql.jdbc.Driver");    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web",            "root", "");    Statement st = con.createStatement();    //ResultSet rs;    int i = st.executeUpdate("insert into class(name, strength ,room, section) values (''" + cname + "'',''" + cstrength + "'',''" + croom + "'',''" + csection + "'', CURDATE());");    if (i > 0) {        //session.setAttribute("userid", user);        response.sendRedirect("wel.jsp");       // out.print("Registration Successfull!"+"<a href=''index.jsp''>Go to Login</a>");    } else {        response.sendRedirect("index.jsp");    }%>

答案1

小编典典

这是您正在运行的查询:

insert into class(name, strength ,room, section) values (''" + cname + "'',''" + cstrength + "'',''" + croom + "'',''" + csection + "'', CURDATE());")

您提到了要传递的4个列值(class(name, strength ,room, section)),但随后传递了5个值(CURDATE()的附加值)

在表中添加该新列,然后更新查询以使其也包含该列(即(class(name, strength ,room, section,curdate))),或删除CURDATE()。

MySQL和PHP错误:列数与第1行的值数不匹配

MySQL和PHP错误:列数与第1行的值数不匹配

尝试将表单中的数据插入数据库时​​出现此错误。我知道这意味着什么,我只是想不出为什么要得到它。也许我已经开始太久了,错过了什么?

这是我的代码:

<?php
            $q1 = mysql_escape_string($_POST['q1']);
            $q2 = mysql_escape_string($_POST['q2']);
            $q3 = mysql_escape_string($_POST['q3']);            
            $q4 = mysql_escape_string($_POST['q4']);
            $q5 = mysql_escape_string($_POST['q5']);            
            $q6 = mysql_escape_string($_POST['q6']);
            $q7 = mysql_escape_string($_POST['q7']);
            $q8 = mysql_escape_string($_POST['q8']);
            $q9 = mysql_escape_string($_POST['q9']);
            $q10 = mysql_escape_string($_POST['q10']);
            $q11a = mysql_escape_string($_POST['q11a']);
            $q11b = mysql_escape_string($_POST['q11b']);
            $q11c = mysql_escape_string($_POST['q11c']);
            $q11d = mysql_escape_string($_POST['q11d']);
            $q11e = mysql_escape_string($_POST['q11e']);
            $q11f = mysql_escape_string($_POST['q11f']);
            $q11g = mysql_escape_string($_POST['q11g']);
            $q11h = mysql_escape_string($_POST['q11h']);            
            $q12 = mysql_escape_string($_POST['q12']);
            $q13 = mysql_escape_string($_POST['q13']);
            $q14a = mysql_escape_string($_POST['q14a']);
            $q14b = mysql_escape_string($_POST['q14b']);
            $name = mysql_escape_string($_POST['name']);            
            $email = mysql_escape_string($_POST['email']);


            require_once('connection.php');

            $sql="INSERT INTO survey (Question1,Question2,Question3,Question4,Question5,Question6,Question7,Question8,Question9,Question10,Question11A,Question11B,Question11C,Question11D,Question11E,Question11F,Question11G,Question11H,Question12,Question13,Question14A,Question14B,name,email) VALUES ('$q1','$q2','$q3','$q4','$q5','$q6','$q7','$q8','$q9','$q10','$q11a','$q11b','$q11c','$q11d','$q11e','$q11f','$q11g','$q11h','$q12','$q13','$q14a','$q14b' '$name','$email')";

            if (!mysql_query($sql,$conn))
              {
              die('Error: ' . mysql_error());
              }

              mysql_close($conn);

    ?>

Mysql和PHP错误:列计数与第1行的值计数不匹配

Mysql和PHP错误:列计数与第1行的值计数不匹配

尝试将表单中的数据插入数据库时​​,我收到此错误.我知道这意味着什么,我只是想不通为什么我会得到它.也许我已经开始了很长时间并错过了一些东西?

这是我的代码:

<?PHP
            $q1 = MysqL_escape_string($_POST['q1']);
            $q2 = MysqL_escape_string($_POST['q2']);
            $q3 = MysqL_escape_string($_POST['q3']);            
            $q4 = MysqL_escape_string($_POST['q4']);
            $q5 = MysqL_escape_string($_POST['q5']);            
            $q6 = MysqL_escape_string($_POST['q6']);
            $q7 = MysqL_escape_string($_POST['q7']);
            $q8 = MysqL_escape_string($_POST['q8']);
            $q9 = MysqL_escape_string($_POST['q9']);
            $q10 = MysqL_escape_string($_POST['q10']);
            $q11a = MysqL_escape_string($_POST['q11a']);
            $q11b = MysqL_escape_string($_POST['q11b']);
            $q11c = MysqL_escape_string($_POST['q11c']);
            $q11d = MysqL_escape_string($_POST['q11d']);
            $q11e = MysqL_escape_string($_POST['q11e']);
            $q11f = MysqL_escape_string($_POST['q11f']);
            $q11g = MysqL_escape_string($_POST['q11g']);
            $q11h = MysqL_escape_string($_POST['q11h']);            
            $q12 = MysqL_escape_string($_POST['q12']);
            $q13 = MysqL_escape_string($_POST['q13']);
            $q14a = MysqL_escape_string($_POST['q14a']);
            $q14b = MysqL_escape_string($_POST['q14b']);
            $name = MysqL_escape_string($_POST['name']);            
            $email = MysqL_escape_string($_POST['email']);


            require_once('connection.PHP');

            $sql="INSERT INTO survey (Question1, Question2, Question3, Question4, Question5, Question6, Question7, Question8, Question9, Question10, Question11A, Question11B, Question11C, Question11D, Question11E, Question11F, Question11G, Question11H, Question12, Question13, Question14A, Question14B, name, email) VALUES ('$q1', '$q2', '$q3', '$q4', '$q5', '$q6', '$q7', '$q8', '$q9', '$q10', '$q11a','$q11b', '$q11c','$q11d', '$q11e', '$q11f','$q11g','$q11h','$q12', '$q13', '$q14a', '$q14b' '$name', '$email')";

            if (!MysqL_query($sql,$conn))
              {
              die('Error: ' . MysqL_error());
              }

              MysqL_close($conn);

    ?>

解决方法:

之间缺少逗号:

'$q14b' '$name'

为什么我们会收到此错误:

考虑一个简单的表temp,其中包含2列v1和varchar类型的v2:

MysqL> desc temp;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| v1    | varchar(10) | YES  |     | NULL    |       |
| v2    | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

现在让我们做2个插入:

MysqL> insert into temp(v1,v2) values('foo' 'bar');  // missing comma..gives err.
ERROR 1136 (21S01): Column count doesnt match value count at row 1

MysqL> insert into temp(v1,v2) values('foo' 'bar','faz'); // missing comma..no er
Query OK, 1 row affected (0.00 sec)

MysqL> select * from temp; // 'foobar' got inserted in v1 !!
+--------+------+
| v1     | v2   |
+--------+------+
| foobar | faz  |
+--------+------+
1 row in set (0.00 sec)

从这些非常明确的MysqL连接两个由空格分隔的相邻字符串,当发生这种情况时,您在值之后提供的参数数量减少1,因此其计数与导致此错误的列数不匹配.

PHP PDOException:“ SQLSTATE [HY093]:无效的参数编号”

PHP PDOException:“ SQLSTATE [HY093]:无效的参数编号”

尝试运行以下功能时,出现错误“ SQLSTATE [HY093]:无效的参数号”:

function add_persist($db,$user_id) {
    $hash = md5("per11".$user_id."sist11".time());
    $future = time()+(60*60*24*14);
    $sql = "INSERT INTO persist (user_id,hash,expire) VALUES (:user_id,:hash,:expire) ON DUPLICATE KEY UPDATE hash=:hash";
    $stm = $db->prepare($sql);
    $stm->execute(array(":user_id" => $user_id,":hash" => $hash,":expire" => $future));
    return $hash;
}

我觉得这很简单,我只是没有抓住。有任何想法吗?

今天关于php – SQLSTATE [HY093]:参数号无效:绑定变量数与第102行的标记数不匹配什么叫sql参数绑定的分享就到这里,希望大家有所收获,若想了解更多关于java.sql.SQLException:列数与第1行错误处的值计数不匹配、MySQL和PHP错误:列数与第1行的值数不匹配、Mysql和PHP错误:列计数与第1行的值计数不匹配、PHP PDOException:“ SQLSTATE [HY093]:无效的参数编号”等相关知识,可以在本站进行查询。

本文标签: