try {
mysql_query("INSERT xyz");
mysql_query("INSERT xyz");
mysql_query("INSERT xyz");
commit()
} catch (Exception $e) {
rollback()
}

I would like so that if any of the queries within the try { } fails, to go to the exception and rollback the transaction, and commit otherwise. What kind of things will it throw an exception for I.E if database is down? etc.

Another thing I've noticed is if something is rolledback, then a new record is made the primary key seems to have auto incremented twice, is this something that can be avoided somehow, or are tables which contains rows like (1, 2, 4, 7) for example bad to have as Primary Keys for any reason?

If someone could offer some advice on these issues it'd make me very happy :)

Member Avatar for LastMitch

asif49

I would like so that if any of the queries within the try { } fails, to go to the exception and rollback the transaction, and commit otherwise.

You are using Exceptions which is good! I guess you read it here:

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

Can you show the function that you are using because it's not very helpful by just using try{}?

Where did you start the transaction? I only see commit and rollback.

I forgot to include some stuff. Here it is :)

function begin() {
mysql_query("BEGIN");
}

function commit() {
mysql_query("COMMIT");
}

function rollback() {
mysql_query("ROLLBACK");
}

.....

try {
begin()
mysql_query("INSERT xyz");
mysql_query("INSERT xyz");
mysql_query("INSERT xyz");
commit()
} catch (Exception $e) {
rollback()
}

What kind of things will be caught here as exceptions? Do I need to add any other error detection methods?

Yes. Mysql doesn't throw exceptions, it returns false. If you want exceptions use PDO.

I haven't been able to find any good examples of how PDO works or what it is. Could you perhaps give an explanation?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.