I have a PHP psql query. I am doing try catch to prevent Duplicates. When I ran the script I can see it's inserting something into my DB. But when I check my DB it's empty.
foreach($data as $n){
$query = $psql->pdo_prepared("INSERT INTO students(id,email,address,phone)VALUES".myFunction($array);
}
and I have a PHP class to handle the exception:
public function pdo_prepared($query,array){
try{
// some logic
}
catch(EXCEPTION $e){
//empty
}
}
The reason why I am doing try catch is to catch duplicates and ignore it. If I throw an exception in my catch block my insert statement won't execute because my current data contains duplicates.
Related
I am connecting to neo4j the normal way and i can run queries no problem.
During testing, i wrote a query that should fail (due to uniqueness constraint), the query does fail as expected and i catch the exception.
The problem is when i try to execute the next query in the queue, it just hangs (longer than timeout).
I don't suppose that is normal behavior.
try{
$result = $neo->run ($query);
}
catch (Exception $e) {
// handle it
}
// all good so far
// now we attempt:
try{
$result = $neo->run ($next_query);
}
catch (Exception $e) {
// handle it
}
// hangs longer than timeout
if i remove the failed query from the queue, everything completes
So it seems that the exception thrown by the php-client breaks the connection to neo4j.
If i modify the code to the following, it all works fine.
try{
$result = $neo->run ($query);
}
catch (Exception $e) {
// handle it
connect_to_neo()
}
// all good so far
try{
$result = $neo->run ($next_query);
}
catch (Exception $e) {
// handle it
}
// all good, $next_query gets executed
I do not think that an exception that breaks the connection is desired behavior. Will raise the issue on github.
I've got a (example) Oracle Stored Procedure:
CREATE OR REPLACE FUNCTION EXCEPTION_TEST
RETURN NUMBER
AS
BEGIN
raise_application_error(-20500, 'This is the exception text I want to print.');
END;
and I call it in PHP with PDO with the following code:
$statement = $conn->prepare('SELECT exception_test() FROM dual');
$statement->execute();
The call of the function works perfectly fine, but now I want to print the Exception text only.
I read somewhere, that you should not use try and catch with PDO. How can I do this?
You have read that you shouldn't catch an error to report it.
However, if you want to handle it somehow, it's all right to catch it.
Based on the example from my article on handling exception in PDO,
try {
$statement = $conn->prepare('SELECT exception_test() FROM dual');
$statement->execute();
} catch (PDOException $e) {
if ($e->getCode() == 20500 ) {
echo $e->getmessage();
} else {
throw $e;
}
}
Here you are either getting your particular error or re-throwing the exception back to make it handled the usual way
You check the execute response and get the error, for example, like this:
if ($statement->execute() != true) {
echo $statement->errorCode();
echo $statement->errorInfo();
}
You can find more options at the PDO manual.
I have an insert trigger in Microsoft SQL Server that raise an error in some situations. like this:
CREATE TRIGGER [dbo].[TriggerName]
ON [dbo].[TableName]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
...
IF (some conditions)
BEGIN
RAISERROR('Error Message',16,10);
ROLLBACK TRANSACTION;
RETURN;
END;
...
END;
I want to get the error in PHP. my php code is similar to this:
...
mssql_min_error_severity(1);
mssql_min_message_severity(1);
$query="INSERT INTO [dbo].[TableName] (...) VALUES (...)"; //values satisfy the error condition
if (mssql_query($query))
echo "No error";
else
echo "Error occured.";
//result is: No error
I expect to get 'Error occured' but I get 'No error' and no row is inserted in the table due to the ROLLBACK command.
What can I do to get an error when I try to insert not allowed values to the table?
include within a try catch
try {
//actions php
}
catch (Exception $e) {
throw $e; //get raise from sql
}
Finally I found the solution:
function error_occured($errorno,$errorstr)
{
throw new Exception($errorstr);
}
...
$error_rep=error_reporting(E_ALL);
set_error_handler("error_occured",E_ALL);
try
{
$result=mssql_query($query);
}
catch (Exception $e)
{
$result=false;
}
error_reporting($error_rep);
restore_error_handler();
...
But this solution don't seems to be a proper way.
I am searching for a better one.
I am trying to catch an error with php when I connect to DB
I have something like
try{
//connect to DB
}catch(exception $e){
echo $e
}
//other php codes...
//My html elements...
<div>....
My problem is that I want to skip //other phpo codes if we have error connecting to DB and straight to show my html elements. Is that possible to do it? Thanks a lot.
Just out that code in your try/catch. Once the exception is thrown execution is handed off to the catch portion of the control structure and that portion of code is never reached:
try{
//connect to DB
// If an exception is throw above we never get here
//other php codes...
}catch(exception $e){
echo $e
}
//My html elements...
<div>....
If you don't want to move the // other php code
And you don't want/can't edit the try/catch block, surely the try/catch returns some variable you can test, even if only that $e.
try {
// something like $connected_db should be available
}
catch (exception $e)
{
}
if (!empty($connected_db) AND empty($e)) // one or the other depending on the code above
{
// other php code
}
// my html elements
I am trying to implement transactions to Kohana but it seems to be not so easy as in Spring/Java.
So far I found this code to try but I don't know how to replace the part (no errors)
DB::query('START TRANSACTION');
// sql queries with query builder..
if (no errors)
DB::query('COMMIT');
else
DB::query('ROLLBACK');
How do I make the if clause?
Normally transactions are handled as such:
DB::query('START TRANSACTION');
try {
//do something
DB::query('COMMIT');
} catch (Exception $e) {
DB::query('ROLLBACK');
}
What this means is if everything succeeds within the try block, great. If any part of it fails then it won't reach the commit and will jump to the catch block, which contains the rollback. You can add more error handling within the catch if you wish, even throw a new exception of your own or throw the same exception you caught.
Just wrap everything in a try/catch block:
DB::query('START TRANSACTION');
try {
// sql queries with query builder..
DB::query('COMMIT');
} catch (Database_Exception $e) {
DB::query('ROLLBACK');
}
DB errors are converted to exceptions:
DB::query('START TRANSACTION');
try {
// sql queries with query builder..
DB::query('COMMIT');
}
catch($e)
{
$this->template->body = new View('db_error');
$this->template->body->error = 'An error occurred ...';
DB::query('ROLLBACK');
}
If you're using Kohana 3:
$db = Database::instance();
$db->begin();
try
{
// Do your queries here
$db->commit();
}
catch (Database_Exception $e)
{
$db->rollback();
}