Solved

Multiple PDOException after each execute

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

12 Experts available now in Live!

Get 1:1 Help Now