【问题标题】:php execute variable undefined in try block that sets prepare statementphp在设置prepare语句的try块中执行未定义的变量
【发布时间】: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 中抛出

标签: php mysql pdo


【解决方案1】:

您错误地使用了if 语句。没有花括号的if 语句{} then 语句仅在“if”条件受到影响之后。

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

【讨论】:

  • 指出得很好。我自动假设发布者知道这一点。仅供参考,这很重要,因为如果 $db 没有设置,那么 PHP 将不会执行 $db->prepare,但会在 ; 之后继续执行下一行。这是 $tools->bindParam 并且需要定义 $tools。此时,它尚未定义并导致描述的错误。
  • 如果我设置了if (!isset($db)) 那么$db->prepare 是未定义的.. 所以我不太明白:/
  • 它解释说,如果$db 没有设置,它可能是undefined。检查你的connection.php 脚本,看看如何定义$db
  • 如果有帮助,这里有一个脚本链接connection.php
    然后是函数function.php
  • 接下来我会检查连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多