Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multiple PDOException after each execute

Posted on 2011-02-28
4
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

704 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