Php PDO查询执行invaid,bindValue不匹配的问题
在PDO和PHP中,我通过一些研究得出了一个查询语句函数的可行解决方案。现在它与语句一起出错Php PDO查询执行invaid,bindValue不匹配的问题,php,mysql,pdo,Php,Mysql,Pdo,在PDO和PHP中,我通过一些研究得出了一个查询语句函数的可行解决方案。现在它与语句一起出错 PHP警告:PDOStatement::execute():SQLSTATE[HY093]:无效参数编号:绑定变量的数量与第39行/var/www/html/PHP/PHP_tools/private/functions.PHP中的令牌数量不匹配 这是我的草稿文件的第15行,为了使它更容易阅读,而不是通过多个文件,它仍然工作相同,我检查 <?php try { $db = new PDO(
PHP警告:PDOStatement::execute():SQLSTATE[HY093]:无效参数编号:绑定变量的数量与第39行/var/www/html/PHP/PHP_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循环中中断。
随着我学习的深入。做更多的研究我相信我的底线“bindValue”是罪魁祸首吗?但我不确定我会用什么作为PDO:?将其宣布为..的项目?任何帮助或指导都是有益的。。因为我可能离……还是新的,还有什么反馈吗?肯定会的
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();
}
您试图传入绑定的所有部分,但试图单独绑定它们。您需要将语句的所有部分传递到一个绑定值中:
foreach ($statements as $statement) {
$query_string->bindValue($statement['string'], $statement['value'], $statement['type']);
}
你把你的函数弄得太复杂了,以至于你自己没能使用它。使其更简单,如下所示:
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();
感谢buddy@sirko您的查询中只有1个绑定,这是一个命名绑定。所以你不能把变量绑定到不存在的占位符上,而且,据我所知,你不能把一个盲变量/数字变量绑定到一个命名变量上,反之亦然。这确实很好地解决了问题。使事情变得更加顺利。我一直在尝试尝试使用try/catch,不同的方法我也很新…是的…过度复杂化是我最擅长的。。。这对SQL注入安全吗?我只是不想受到攻击。但是谢谢你所做的一切:)只需从侧边栏查看我的文章以及相关文章。