Solved

Multiple PDOException after each execute

Posted on 2011-02-28
4
171 Views
Last Modified: 2012-05-11
Hi,

I have this bit of code and I'm wondering if I can use multiple PDOException after each execute function since there are several SELECT statements. There is an error in my PHP IDE (using nusphere) when I add in the:
catch(PDOException $e) {...}

Open in new window


after each execute()

Thank you,
Victor
/**
      * Get selected product from products table for the given product_id
      * @return int (success == 1), array $row[all fields from products table], int $count
      * @param product_id (primary key to product_id.products)
      * @access public
      */
      public function getSelectedProductInfo($product_id) {

         try {
            /* Verify connection */
            $q = "SELECT * FROM products where product_id=".$product_id;
            $stmt = db::getConnect()->prepare($q);
            $countTest = $stmt->execute();

            //return count
            $count = $stmt->rowCount();

            //success
            if ($count >= 1) {
               $row = $stmt->fetchAll(PDO::FETCH_ASSOC);

               /**
               * Get product_type_name form product_types table 
               * (described as Category: on product-scores.php page)
               * product_type.products = product_type_id.product_types
               * @param int product_type.products ($row['product_type'])
               */
               $product_type = $row['product_type'];
               $q2 = "SELECT product_type_name FROM product_types where product_type_id=".$product_type;
               $stmt2 = db::getConnect()->prepare($q2);
               $stmt2->execute();
               $countProdTypeName = $stmt2->rowCount(); //return count to verify

               if ($countProdTypeName >= 1) {
                  $rowProdTypeName = $stmt2->fetchAll(PDO::FETCH_ASSOC);
               }                

               /**
               * Get brand_name form product_brands table 
               * (described as Brand: on product-scores.php page)
               * product_brand.products = brand_id.product_brands
               * @param int product_brand.products ($row['product_brand'])
               */
               $product_brand = $row['product_brand'];
               $q3 = "SELECT brand_name FROM product_brands where brand_id=".$product_brand;
               $stmt3 = db::getConnect()->prepare($q3);
               $stmt3->execute();
               $countBrandName = $stmt3->rowCount(); //return count to verify

               if ($countBrandName >= 1) {
                  $rowBrandName = $stmt3->fetchAll(PDO::FETCH_ASSOC);
               }   

               /**
               * Get sub_brand_name form product_sub_brands table
               * (described as Product Line: on product-scores.php page)
               * product_sub_brand.products = sub_brand_id.product_sub_brands
               * @param int product_sub_brand.products ($row['product_sub_brand']) 
               */
               $product_sub_brand = $row['product_sub_brand'];
               $q4 = "SELECT sub_brand_name FROM product_sub_brands where sub_brand_id=".$product_sub_brand;
               $stmt4 = db::getConnect()->prepare($q4);
               $stmt4->execute();
               $countSubBrandName = $stmt4->rowCount(); //return count to verify

               if ($countSubBrandName >= 1) {
                  $rowSubBrandName = $stmt4->fetchAll(PDO::FETCH_ASSOC);
               }

               return array(1, $row, $count, $rowProdTypeName, $rowBrandName, $rowSubBrandName);
            }
         }
         catch(PDOException $e) {
            $count = 0;
            return array(0, $e->getMessage(), $count);
         }
      }

Open in new window

0
Comment
Question by:Victor Kimura
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 34996161
You could try to throw new exception after each select
like:
$stmt2->execute() or throw new Exception('something wrong here STMT2.');
0
 

Author Comment

by:Victor Kimura
ID: 34996761
So starting from line 28 could I do something like this?
$product_type = $row['product_type'];
               $q2 = "SELECT product_type_name FROM product_types where product_type_id=".$product_type;
               $stmt2 = db::getConnect()->prepare($q2);
               $stmt2->execute();
               $countProdTypeName = $stmt2->rowCount(); //return count to verify

               if ($countProdTypeName >= 1) {
                  $rowProdTypeName = $stmt2->fetchAll(PDO::FETCH_ASSOC);
               }  throw new Exception('something wrong here STMT2.');

Open in new window


Doing the following gives me an error:
 if ($countProdTypeName >= 1) {
                  $rowProdTypeName = $stmt2->fetchAll(PDO::FETCH_ASSOC);
               } catch(PDOException $e) {
            $count = 0;
            return array(0, $e->getMessage(), $count);
         }

Open in new window


I'm using PDO for the connection handle.
0
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 500 total points
ID: 34996855
Rather on execute:

               $product_type = $row['product_type'];
               $q2 = "SELECT product_type_name FROM product_types where product_type_id=".$product_type;
               $stmt2 = db::getConnect()->prepare($q2);
// BELOW
               $stmt2->execute() or throw new Exception('something wrong here STMT2.');
               $countProdTypeName = $stmt2->rowCount(); //return count to verify

               if ($countProdTypeName >= 1) {
                  $rowProdTypeName = $stmt2->fetchAll(PDO::FETCH_ASSOC);
               } 

Open in new window


Throwing an exception goes out of the try statement.
0
 

Author Comment

by:Victor Kimura
ID: 35002727
Thanks, Roads Roads.

So this line here:

$stmt2->execute() or throw new Exception('something wrong here STMT2.');

Open in new window


how do I return the error message in the array like I do with the catch in this code:

catch(PDOException $e) {
            $count = 0;
            return array(0, $e->getMessage(), $count);
         }

Open in new window


Thanks for your help.

Victor
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now