473,396 Members | 1,996 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,396 software developers and data experts.

Transactions in PHP on SQL Server 2005

I'm trying to get a transaction to work with PHP 5.1.4 and SQL Server
2005... The first SQL statement deletes several rows in the database.
The next SQL statement loops through and inserts several rows into the
same table. What I'm wanting is for the database to rollback all
transactions if any 1 of the inserts fail. What's happening now is the
DELETE statement runs successfully deleting several rows... but as soon
as one of the INSERTS fail, the page errors out WITHOUT rolling back
the DELETE statements. What am I doing wrong in my code?

thanks,

~john

mssql_query("BEGIN TRAN");

$sql = "DELETE FROM MyTable WHERE Value 0";

$result = mssql_query($sql);

foreach(loopVar as val)
{
$sql = "INSERT INTO MyTable VALUES(1)";
$result = mssql_query($sql);

if( ! $result ){
mssql_query('ROLLBACK TRAN');
exit;
}

}

mssql_query("COMMIT TRAN");

Jul 14 '06 #1
4 2319
~john wrote:
I'm trying to get a transaction to work with PHP 5.1.4 and SQL Server
2005... The first SQL statement deletes several rows in the database.
The next SQL statement loops through and inserts several rows into the
same table. What I'm wanting is for the database to rollback all
transactions if any 1 of the inserts fail. What's happening now is the
DELETE statement runs successfully deleting several rows... but as soon
as one of the INSERTS fail, the page errors out WITHOUT rolling back
the DELETE statements. What am I doing wrong in my code?

thanks,

~john

mssql_query("BEGIN TRAN");

$sql = "DELETE FROM MyTable WHERE Value 0";

$result = mssql_query($sql);

foreach(loopVar as val)
{
$sql = "INSERT INTO MyTable VALUES(1)";
$result = mssql_query($sql);

if( ! $result ){
mssql_query('ROLLBACK TRAN');
exit;
}

}

mssql_query("COMMIT TRAN");
John,

Why not try asking in a MySQL newsgroup, like comp.databases.mysql?
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 15 '06 #2
Jerry Stuckle wrote:
John,

Why not try asking in a MySQL newsgroup, like comp.databases.mysql?


SQL Server... but anyway, I'm asking because the calling code is PHP.
The SQL by itself runs fine in SQL Server's query analzyer.

~john

Jul 15 '06 #3
~john wrote:
Jerry Stuckle wrote:

>>John,

Why not try asking in a MySQL newsgroup, like comp.databases.mysql?


SQL Server... but anyway, I'm asking because the calling code is PHP.
The SQL by itself runs fine in SQL Server's query analzyer.

~john
John,

Sorry - shouldn't try to replay late at night. I say mysql when you
were saying mssql.

OK, it works with SQL Server's query analyzer, so your logic is OK. But
it looks you're not actually using transactions - it's seems to be
committing after each request. There are no options in PHP to control
this. But what about SQL Server?

I would still start with a SQL Server newsgroup. That's where you're
seeing the problem, not in PHP. And that's where your troubleshooting
should start.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 15 '06 #4
***UPDATE***

Not sure what I was smokin' that day but the transactions do in fact
appear to be working as expected.

~john

Jul 17 '06 #5

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

Similar topics

9
by: TD | last post by:
I am trying to add transactions to my code. The original code worked fine until I followed an example to setup transactions, now the code does strange things, but no error messages. Could...
3
by: Ace Calhoon | last post by:
Hello, I have a VBA/Database application which reads files, analyzes them, updates a database, and then moves them to an archive. I would like to make this an atomic transaction -- that is, if...
11
by: Mike P | last post by:
I've been using C# transactions for a while and had no problems with them. Using try catch blocks I can trap basically all possible errors and rollback all necessary data. Over the last few...
1
by: rlm | last post by:
When I attempt to manage Transactions in a WHILE LOOP @@TRANCOUNT is off. I obviously do not understand error handling as I should. In the loop below where does the point of execution move to after...
1
by: Nacho | last post by:
Hello, I'm trying to follow some sql sentences that my system send to SQL 2005 express and I don't have a deep knowlegde of databases. I know that there's a transactions log that keeps all...
1
by: sjoshi | last post by:
Hello I had a question reagrding MSDTC usage. I know that MSDTC will enlist each connection transactions as it's own (take ownership) and commit/abort accordingly. However I'm not sure what...
0
by: nitinsharma717 | last post by:
hi, Front End : Win Forms (Visual Studio 2005) Back End : Ms Sql Server 2005 In the code i have to Show No.of database deadlocks and No. of transactions per second/minute of the Database...
4
by: steven | last post by:
I have a small database that I have been testing. I get an error about a transaction deadlock. The code is in stored procedures and I added transactions to the sp's but the error happened again....
2
by: G.S. | last post by:
Is there a way to use transactions from within C# code without using MSDTC (client-and-server environment where the SQL server is behind a firewall)? Thank you
3
by: psycho | last post by:
I am working on an N-tier application using following components: 1. Data Access Layer using DLINQ which consists of Data Context class and Table Mapping classes. 2. Business Logic Layer....
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.