【发布时间】:2018-06-13 19:06:33
【问题描述】:
如果之前有人问过这个问题,我深表歉意。那时我不能正确地问这个问题。
我在声明 $tools is undefined 的“bindParam”上遇到错误。我不确定,因为我还是 PHP 新手。我很困惑如何或为什么超出范围。我不确定,因为它是一个对象或什么。
function query_group_by_param($param) {
try {
include('connection.php');
if(isset($db))
$tools = $db->prepare("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");
$tools->bindParam(':tool', $param, PDO::PARAM_STR, 15);
$tools->execute();
}catch (PDOException $e) {
echo 'unable to retrieve data';
echo $e->getMessage();
exit();
}
$tool = $tools->fetchAll(PDO::FETCH_ASSOC);
$tools->closeCursor();
return $tool;
}
另外,在include(connection.php') 下没有if(isset($db)
显示为未定义。
任何建议或链接将不胜感激。请。
【问题讨论】:
-
我会鼓励您重新考虑 if 语句 - if(isset($db))。如果未设置 $db,则下一行 $tools = $db->prepare... 将不会执行。我认为检查它的更好方法是检查它是否未设置,然后抛出异常。您可以捕获超过 1 种异常类型,如下所述:stackoverflow.com/questions/8439581/… 为了进一步提供帮助,请发布您的 connection.php(删除所有敏感数据,如密码)并发布您收到的 PHP 错误。
-
这里是 connection.php connection.php 的链接,还有我仍在尝试使用的功能,但有一两条评论,以便您查看。 function.php
-
致命错误:未捕获错误:在 C:\xampp\htdocs\php_tools\private\functions.php:73 中调用成员函数 fetch() 堆栈跟踪:#0 C:\xampp \htdocs\php_tools\details.php(23): single_item_images_query('16') #1 {main} 在第 73 行的 C:\xampp\htdocs\php_tools\private\functions.php 中抛出