pdo query execute invaid,bindvalue不匹配的问题

jaxagkaj  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(309)

在pdo和php中,我通过一些研究得出了一个查询语句函数的可行解决方案。现在它与声明有误
php警告:pdostatement::execute():sqlstate[hy093]:无效参数号:绑定变量的数量与第39行的/var/www/html/php/php\u tools/private/functions.php中的令牌数量不匹配
这是在我的草稿文件的第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循环中中断。随着我学习的深入。做更多的研究我相信我的底部“价值”是罪魁祸首?但我不确定我会用什么作为pdo:?宣布为..的项目?任何帮助或指导都是有益的。。因为我可能离…还是新的,还有什么反馈吗?

p1tboqfb

p1tboqfb1#

肯定会的

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();
}
zzzyeukh

zzzyeukh2#

你把你的函数弄得太复杂了,以至于你自己都没能用上。更简单,像这样:

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();
rt4zxlrg

rt4zxlrg3#

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

foreach ($statements as $statement) {

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

}

相关问题