【问题标题】:Php mysql select statement with variables (Invalid parameter number: number of bound variables does not match number of tokens)带变量的php mysql select语句(参数号无效:绑定变量的数量与标记的数量不匹配)
【发布时间】:2013-06-12 18:19:35
【问题描述】:

需要SELECTmysql数据根据用户输入。

例如,在输入表单(字段)中,用户输入日、月、年。并且需要获取(fetch)与输入的日月年匹配的mysql数据。

这是我的代码:

if (strlen($_POST['date_day'] > 0)) {
$post_date_day = $_POST['date_day'];
$date_day = 'RecordDay = ?';
}
if (strlen($_POST['date_month'] > 0)) {
$post_date_month = $_POST['date_month'];
$date_month = ' and RecordMonth = ?';
}
if (strlen($_POST['date_year'] > 0)) {
$post_date_year = $_POST['date_year'];
$date_year = ' and RecordYear = ?';
}

if ( strlen( $_POST['date_day'] or $_POST['date_month'] or $_POST['date_year'] ) > 0 ) {
$where = 'WHERE';
}

$sql_prepare = $where .$date_day .$date_month .$date_year;
$sql_execute = array($post_date_day, $post_date_month, $post_date_year);


$sql = $db->prepare("SELECT * FROM 2_1_journal $sql_prepare ");
$sql->execute($sql_execute);

如果 $_POST['date_day'] 和 $_POST['date_month'] 和 $_POST['date_year'] 的 strlen 大于 0,则脚本有效。

但是,例如,如果用户将年份留空(因为想要查看与日期和月份匹配的所有结果),我会收到错误 SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens。

不明白为什么令牌数不匹配。因为

if (strlen($_POST['date_year'] > 0)) {
$post_date_year = $_POST['date_year'];
$date_year = ' and RecordYear = ?';
}

我想如果 (strlen($_POST['date_year'] == 0) 那么 $post_date_year 和 $date_year 是空白的;所以没有变量,也没有令牌。

可能我的解决方法是错误的,有一些好的工作方法.....

【问题讨论】:

  • strlen($_POST['date_year']) > 0

标签: php mysql select where


【解决方案1】:

当参数可用时,您会发现在创建动态查询以添加到基本查询时会更容易。在第一个参数之后使用标志从 WHERE 切换到 AND。该值同时添加到数据数组中。

如果没有传递参数并且$data[] 为空,则添加错误捕获。

$post_date_day =$_POST['date_day'];
$post_date_month = $_POST['date_month'];
$post_date_year = $_POST['date_year'];
$data = array();
$query = "SELECT * FROM 2_1_journal";
$flag = 0;//Set as WHERE
if (strlen($post_date_day)> 0) {
$query = $query." WHERE RecordDay = ?";
$data[]  =  $post_date_day;//Add to array
$flag = 1;//Switch to AND
}
if (strlen($post_date_month) > 0) {
if($flag ==0){
    $query.= " WHERE RecordMonth = ?";
    $flag = 1;//Switch to AND
  }else{
    $query.= " AND RecordMonth = ?";
  }
  $data[]  = $post_date_month;//Add to array
}
if (strlen($post_date_year) > 0) {
if($flag ==0){
    $query.= " WHERE RecordYear = ?";
  }else{
    $query.= " AND RecordYear = ?";
  }
  $data[]  = $post_date_year;//Add to array
}
$sql = $db->prepare($query);
if (empty($data)){//If no parameters passed
    $sql->execute();   
}else{
    $sql->execute($data); 
}

【讨论】:

    【解决方案2】:

    这是因为你使用了错误的圆括号

    (strlen($_POST['date_year'] > 0))
    

    您应该计算 post 变量,因此用圆括号括住 $_POST['date_year'],然后与 0 进行比较

    (strlen($_POST['date_year']) > 0)
    

    【讨论】:

    • 感谢您的建议。我的愚蠢错误。但是我得到错误。我试过 print_r($sql_execute);并得到 Array ( [0] => 05 [1] => 01 [2] => ) 所以这意味着 [2] => 存在?
    • 是的,这是因为您创建了一个包含三个元素的数组,并且您将这些元素与变量一起传递,这些变量可能是空的,因为关于 $_POST 变量的 if 语句
    • 无法理解您对使用变量传递元素的评论(因为 if 语句)......这里有什么解决方案吗?
    • $sql_execute = array($post_date_day, $post_date_month, $post_date_year); 在这里您传递了三个变量,但它们可能为空,因为它们充满了 if 语句。因此,无论变量是否为空,这都会创建一个包含三个元素的数组。
    • 我改成$sql_execute = array($_POST['date_day'], $_POST['date_month'], $_POST['date_year']); 结果一样....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多