Solved

Need to display errors on a bulk insert loop, but then continue processing inserts

Posted on 2006-07-20
12
208 Views
Last Modified: 2013-12-12
I'm doing a bulk insert of data into a postgreSQL db using PHP.  What I would like to do is display any errors, and then continue processing.  I am checking for the obvious things before attempting the insert, but things like unique key violation in the database can still cause the insert to fail.

Can someone please help me to display the error, and then continue on.  Here's my code:

                  foreach ($lines as $line) {
            $customer_record = explode(",",trim($line));
            $cc = $customer_record[0];
            $first = $customer_record[1];
            $surn = $customer_record[2];
            $salut = $customer_record[3];
            $email = $customer_record[4];
            if ($email != '') {
                  $query = "select * from customer_new($myCID, '$cc', '$first', '$surn', '$salut', '$email')";
                  $result = my_query($dbconn,$query,'Adding records','error.html'); // ********** this is where it currently falls over.
                  if ($row = pg_fetch_assoc($result)) {  
                          $count = $count + 1;
                              } else {
                       echo "<p>Error occured when loading: ".$first." ".$surn." - ".$email."</p>";  // **** this never executes
                  }
            } else {
                  echo "<p>Error:  The email address for $first $surn was empty.</p>";
            }
            $email = '';
            
      }

Thanks heaps!
0
Comment
Question by:hmaloney
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 250 total points
ID: 17151432
You can catch exceptions using
Try and Catch blocks

The manual uses the below example;

try {
   $error = 'Always throw this error';
   throw new Exception($error);

   // Code following an exception is not executed.
   echo 'Never executed';

} catch (Exception $e) {
   echo 'Caught exception: ',  $e->getMessage(), "\n";
}

http://au.php.net/manual/en/language.exceptions.php

You can use this around your queries that are failing and write $e->getMessage() to an array $error[] =
And then loop through all the error messages
0
 

Author Comment

by:hmaloney
ID: 17151461
I've already tried using try catch and it ignores it... I've also read within Experts Exchange that exceptions thrown by a DB update not actually exceptions in the PHP sense, and therefore this wont work.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 17152291
You are using a SELECT, this is NOT going to update anything!!!
0
 

Author Comment

by:hmaloney
ID: 17152317
No I'm not.  :-)

There's an inbuilt function (abstraction) being called which looks like a select, but it is actually doing an insert.

Please ignore that part of the code; it's all working.

The bit that isn't is trying to stop the page from crashing when one particular call to the database function doesn't work.

Thanks for looking though!
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 17152471
What is the value of $result when the query fails?
0
 
LVL 6

Expert Comment

by:SysTurn
ID: 17152643
You can use the @ character with my_query() function to force PHP ignore any errors inside that function and continue the loop

@my_query($dbconn,$query,'Adding records','error.html');
0
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.

 
LVL 40

Expert Comment

by:RQuadling
ID: 17152668
No @ will NOT force PHP to "ignore" the errors. If a fatal error occurs in the execution, it will terminate.

The @ only suppresses the message. The error STILL HAPPENED. You just told PHP to bury its head in the sand!
0
 
LVL 6

Accepted Solution

by:
SysTurn earned 150 total points
ID: 17152711
@RQuadling : I know it's will not ignore Fatal Errors, but the the "DB update error" is not a Fatal Errors which will happen as I said inside the my_query() function, so I guess it can help. and I dont also think there are Fatal Errors inside that function or the Try Catch thing would work in the first place.

Kind Regards
Bakr
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 17152845
hmaloney, what actually happens at the moment?
0
 

Author Comment

by:hmaloney
ID: 17153072
The @ wont work in front of the my_query command.  

I've gone back to the programmer who put together the my_query functionality, and I'm expecting that they will be able to provide me with the code to handle the errors.
0
 
LVL 40

Assisted Solution

by:RQuadling
RQuadling earned 100 total points
ID: 17153133
The @ will only suppress PHP's own error messaging. If the function raises PHP errors,then @ will work.

What is the value of $result when the code fails? That is what you need to test.

Maybe just output it for the time being ...

echo var_export($result, True) . '<br />';

sort of thing.

That way you can see what you need to test to determine if it worked or not.
0
 

Author Comment

by:hmaloney
ID: 17162835
The code that I was using was changed so that errors from the database are handled better, and the loop can move onto the next without exiting.  

Thanks for all your input!
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

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…
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…
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.
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 …

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

26 Experts available now in Live!

Get 1:1 Help Now