Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multiple PDOException after each execute

Posted on 2011-02-28
4
Medium Priority
?
198 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 2000 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

971 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