Solved

Multiple PDOException after each execute

Posted on 2011-02-28
4
176 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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.

786 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