473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

error during a transaction gets a php script to abort

Hello dear friends,
I am writing a small program to test transactions in php. But when I
try to simulate an error condition during transaction, my php script
aborts. It does

rollback work, but I cannot continue the script. Please help. I am a
newbie to both php and postgresql.

PostgreSQL 7.4.17
PHP 5.1.6

table 1 - users:- username(unique), password, handle(unique).
table 2 - contacts:- username(unique), email(unique), foreign key -
users(username).

When I try to insert into a contacts with value of email which already
exist, it fails due to unique contact. The query should fail and I
should enter into

the $result === FALSE block. But it just displays the following and
ends the script.

PHP Warning: pg_query(): Query failed: ERROR: duplicate key violates
unique constraint "contacts_email_key" in /tmp/transaction.php on line
80
Failed to execute command2.

The code.

$dbConnection = sqlConnect("localhost", "vivek", "postgres");
if ($dbConnection === FALSE) {
echo "Program Failed.\n";
return;
}
echo "Connection id returned is " . $dbConnection . ".\n\n";

// construct the command.
$command1 = "Insert into users (username,passwd,handle) VALUES ('foo',
'foo', iamfoo')";
$command2 = "Insert into contacts (username,email) VALUES ('foo',
'f**@site.com')";
echo "Command 1 is:- " . $command1 . "\n";
echo "Command 2 is:- " . $command2 . "\n";

$result = pg_query($dbConnection, "begin");
if ($result === FALSE) {
echo "Unable to begin a transaction. " . pg_last_error() . ".
\n";
return;
}

pg_free_result($result);
$result = pg_query($dbConnection, $command1);
if ($result === FALSE) {
echo "Failed to execute command1.\n";
$result = pg_query($dbConnection, "rollback");
if ($result === FALSE) {
echo "Failed to rollback\n";
}
return;
}
pg_free_result($result);

$result = pg_query($dbConnection, $command2);
if ($result === FALSE) {
echo "Failed to execute command2.\n";
$result = pg_query($dbConnection, "rollback");
if ($result === FALSE) {
echo "Failed to rollback\n";
}
return;
}
pg_free_result($result);

$result = pg_query($dbConnection, "commit");
if ($result === FALSE) {
echo "Failed to commit.\n";
$result = pg_query($dbConnection, "rollback");
if ($result === FALSE) {
echo "Failed to rollback\n";
}
return;
}

// Free the result set.
pg_free_result($result);

// Closing connection
sqlDisconnect($dbConnection);

Jun 28 '07 #1
4 2737
On 28 Jun, 11:27, wizard <vivek.j.jo...@gmail.comwrote:
Hello dear friends,
I am writing a small program to test transactions in php. But when I
try to simulate an error condition during transaction, my php script
aborts. It does

rollback work, but I cannot continue the script. Please help. I am a
newbie to both php and postgresql.

PostgreSQL 7.4.17
PHP 5.1.6

table 1 - users:- username(unique), password, handle(unique).
table 2 - contacts:- username(unique), email(unique), foreign key -
users(username).

When I try to insert into a contacts with value of email which already
exist, it fails due to unique contact. The query should fail and I
should enter into

the $result === FALSE block. But it just displays the following and
ends the script.

PHP Warning: pg_query(): Query failed: ERROR: duplicate key violates
unique constraint "contacts_email_key" in /tmp/transaction.php on line
80
Failed to execute command2.

The code.

$dbConnection = sqlConnect("localhost", "vivek", "postgres");
if ($dbConnection === FALSE) {
echo "Program Failed.\n";
return;}

echo "Connection id returned is " . $dbConnection . ".\n\n";

// construct the command.
$command1 = "Insert into users (username,passwd,handle) VALUES ('foo',
'foo', iamfoo')";
$command2 = "Insert into contacts (username,email) VALUES ('foo',
'...@site.com')";
echo "Command 1 is:- " . $command1 . "\n";
echo "Command 2 is:- " . $command2 . "\n";

$result = pg_query($dbConnection, "begin");
if ($result === FALSE) {
echo "Unable to begin a transaction. " . pg_last_error() . ".
\n";
return;

}

pg_free_result($result);

$result = pg_query($dbConnection, $command1);
if ($result === FALSE) {
echo "Failed to execute command1.\n";
$result = pg_query($dbConnection, "rollback");
if ($result === FALSE) {
echo "Failed to rollback\n";
}
return;}

pg_free_result($result);

$result = pg_query($dbConnection, $command2);
if ($result === FALSE) {
echo "Failed to execute command2.\n";
$result = pg_query($dbConnection, "rollback");
if ($result === FALSE) {
echo "Failed to rollback\n";
}
return;}

pg_free_result($result);

$result = pg_query($dbConnection, "commit");
if ($result === FALSE) {
echo "Failed to commit.\n";
$result = pg_query($dbConnection, "rollback");
if ($result === FALSE) {
echo "Failed to rollback\n";
}
return;

}

// Free the result set.
pg_free_result($result);

// Closing connection
sqlDisconnect($dbConnection);
Do not multi-post. Cross-post if you must but don't multi post
http://www.blakjak.demon.co.uk/mul_crss.htm

Jun 28 '07 #2
rf

"Captain Paralytic" <pa**********@yahoo.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
On 28 Jun, 11:27, wizard <vivek.j.jo...@gmail.comwrote:
>Hello dear friends,
Do not multi-post. Cross-post if you must but don't multi post
and snip the portions of the post you are replying to that are not relevant
so others to not have to search through pages of stuff just to find your one
line reply.

--
Richard.
Jun 28 '07 #3
wizard wrote:
But it just displays the following and ends the script.

PHP Warning: pg_query(): Query failed: ERROR: duplicate key violates
unique constraint "contacts_email_key" in /tmp/transaction.php on line
80
Failed to execute command2.
From the code posted, that is to be expected.

The pg_query issues a warning, but continues. If you want to suppress this
warning, then use the @-operator. The $result===FALSE condition evaluates
to TRUE, so the interpreter executes the conditional block, which does the
following:

* prints out "Failed to execute command2"
* rolls back the transaction
* executes a "return" instruction

At a guess, "return" doesn't mean what you think it means. "return" means
"I'm finished with this function/script. Stop processing any more of it".
That is why the script finishes here.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 7 days, 16:33.]

Long-Awaited Zeldman Article
http://tobyinkster.co.uk/blog/2007/0...ldman-in-time/
Jun 28 '07 #4
On 28 Jun, 13:39, "rf" <r...@invalid.comwrote:
"Captain Paralytic" <paul_laut...@yahoo.comwrote in message

news:11*********************@k79g2000hse.googlegro ups.com...
On 28 Jun, 11:27, wizard <vivek.j.jo...@gmail.comwrote:
Hello dear friends,
Do not multi-post. Cross-post if you must but don't multi post

and snip the portions of the post you are replying to that are not relevant
so others to not have to search through pages of stuff just to find your one
line reply.

--
Richard.
Good point. I guess I'm spoilt by the Google reader hiding the quoted
text automagically.

Jun 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Abdul-Wahid Paterson | last post by:
Hi, I have had a site working for the last 2 years and have had no problems until at the weekend I replace my database server with a newer one. The database migration went like a dream and I had...
0
by: Mythran | last post by:
I'm getting this error and Microsoft's site states that the way we are using our libraries is unsupported and the error is by design (go figure). My setup is simple and why it isn't supported is...
0
by: Mart | last post by:
Hi, I have just written (my first) VB.net app using MS Visual Basic 2005 Express Edition Beta. It is fairly simple, it reads some configuration data from an XML file then opens a new window...
0
by: Uma | last post by:
Dear all, I have a problem while running a smart client application which was installed through CD-ROM. After installing the smart client setup the application is running properly. When running...
0
by: processendnow | last post by:
I have a vb.net form application, it works inside Visual Baisc Express Debugger, packages without errors or warning, and is publishable. Click on the setup.exe to install and it installs, but it...
6
by: Josef Brunner | last post by:
Hi, I published my application (VS 2005) and am now trying to install it when I get this error message. It worked before...even on a different machine. Here is the detailed description: ...
0
by: Marty Cruise | last post by:
I successfully deploy my application to 20 domain users. Only one new user is giving me a problem, although he can access all domain resources. When he clicks the installation link on the...
1
by: Chubbly Geezer | last post by:
I have an application that has previously been working correctly. However, having made some code changes today, the app still publishes correctly but when I try to run it I get the following...
2
by: Axel Gallus | last post by:
I am desperately looking for some resources dealing with error handling in c++. Don't get me wrong.... I have very detailed knowledge of exceptions, assertions and invariants, but what is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.