dongqian3198 2018-01-04 04:31
浏览 51

php执行变量undefined在try块中设置prepare语句

I apologize if this has been asked before. I can not ask the question properly then.

I am getting an error on the "bindParam" that states $tools is undefined. I am unsure since I am still new too PHP. I am confused how or why it is out of scope. I am not sure because it is an object or what.

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

Also, Without the if(isset($db) under the include(connection.php') that shows up as if it is undefined.

Any suggestions or links would be greatly appreciated. please.

  • 写回答

1条回答 默认 最新

  • douba3975 2018-01-04 04:47
    关注

    You made a mistake to use a if statement. The if statement without curly braces {} then statement only after the "if" condition get affected.

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

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)