【发布时间】: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 个绑定,即命名绑定。所以你不能将变量绑定到不存在的占位符,据我所知,你不能将盲/数字变量绑定到命名变量,反之亦然。