【问题标题】:PDO query execute invaid, issue with bindValue not matchingPDO 查询执行无效,bindValue 不匹配问题
【发布时间】:2018-06-15 08:18:39
【问题描述】:

在 PDO 和 PHP 中,我进行了一些研究,我认为这对于查询语句函数来说是一个可行的解决方案。现在它的语句出错了

PHP 警告:PDOStatement::execute(): SQLSTATE[HY093]: 无效参数编号:绑定变量的数量与 /var/www/html/php/php_tools/private/functions.php 中的标记数量不匹配第 39 行

它位于我的暂存文件的第 15 行,以使其更易于阅读,而不是处理多个文件,它仍然可以正常工作,我检查了。

 <?php

try {
  $db = new PDO("mysql:host=$host;dbname=$base;", $user, $pass);
}catch(Exception $e){
     echo $error = $e->getMessage();
}

function execute_query($con, $query, $statements) {

$query_string = $con->prepare($query);

foreach ($statements as $statement) {

    $query_string->bindValue(1, $statement['string'], PDO::PARAM_STR);
    $query_string->bindValue(2, $statement['value'], PDO::PARAM_STR);
    $query_string->bindValue(3, $statement['type'], PDO::PARAM_STR);// I believe this is the culprit?

    }

$query_string->execute();


return $query_string->fetchAll();
 }

   $multi_item_query = "SELECT t.t_id as id, t.item_code as code, 
 t.item_name as name,
 t.retail_price as retail, t.sale_price as price,
 t.item_pieces as  pieces, t.qty as quantity,
 t.sold as sold, t.description as description,
 b.brand as brand, c.category as category,
 tt.tool_type as sections, i.image as image
 FROM Tools as t
 INNER JOIN Brands as b on t.b_id = b.b_id
 INNER JOIN Categories as c ON t.c_id = c.c_id
 INNER JOIN Images AS i ON t.t_id = i.t_id
 LEFT OUTER JOIN Types AS tt ON t.tt_id = tt.tt_id
 WHERE tt.tool_type = :tool";

if ( isset($_GET['cat']) ) {
     if ( $_GET['cat'] == 'wrenches') {
        $page_name = 'Wrenches';
        $section = 'wrenches';
        $param = 'wrenches';
    } elseif ( $_GET['cat'] == 'blades') {
         $page_name = 'Blades';
        $section = 'blades';
        $param = 'blades';
    } else {
        $page_name = 'Full Catalog';
         $section = null;
    }
}
 $con = $db;
 $statement = array(); // Prepare a statement array.
$id = array(
     'string' => ':tool',
     'value' =>  $param,
     'type' => PDO::PARAM_STR
 );

 $statement[] = $id;

 ?>

<?php  $items = execute_query($con, $multi_item_query, $statement); ?>

就在“bindValue”处,它现在在 foreach 循环中中断。 随着我学习的更多。做更多的研究我相信我最底层的“bindValue”是罪魁祸首吗?但我不确定我会使用什么作为 PDO::?项目宣布为.. ?任何帮助或指导都是有益的..因为我可能还很遥远..还是新手,还有任何反馈吗?

【问题讨论】:

  • 感谢好友@Sirko
  • 您的查询中只有 1 个绑定,即命名绑定。所以你不能将变量绑定到不存在的占位符,据我所知,你不能将盲/数字变量绑定到命名变量,反之亦然。

标签: php mysql pdo


【解决方案1】:

你让你的函数过于复杂,以至于你自己没有使用它。让它更简单,像这样:

function execute_query($con, $query, $variables) {
    $stmt = $con->prepare($query);
    $stmt->execute($variables)
    return $stmt;
}

这样你就可以运行它了

$con = $db;
$variables['tool'] = $param;
$items = execute_query($con, $multi_item_query, $variables)->fetchAll();

【讨论】:

  • 这真的很清楚。并使事情变得更加顺利。我一直在尝试尝试使用 try/catch,而且不同的方法也很新……是的……过度复杂化是我最擅长的……这对于 SQL 注入是否安全,我只是不想受到攻击.但谢谢你所做的一切:)
  • 只需查看我的PDO tutorial 以及那里侧边栏中的相关文章。
【解决方案2】:

您试图传递绑定的所有部分,但尝试单独绑定它们。您需要将语句的所有部分传递给一个绑定值:

foreach ($statements as $statement) {

    $query_string->bindValue($statement['string'], $statement['value'], $statement['type']);

}

【讨论】:

    【解决方案3】:

    肯定是这样的

    foreach ($statements as $statement) {
        $query_string->bindValue($statement['string'], $statement['value'],$statement['type'] );
    }
    

    以及循环内的execute方法

    foreach ($statements as $statement) {
        $query_string->bindValue($statement['string'], $statement['value'],$statement['type'] );
        $query_string->execute();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多