I am not very good with OOP and I need a script. I tried to do my best, but I can't make it work. Can someone make an OOP script for me (it is a small script).
I need a function startTransaction()
and inside the function I put two query's. I also create a variable inside this function ($transaction_called=1)
and the next time the function is called it should have access to this variable.
I need a second function stopTransaction()
. There are also query's inside this function, and I need access to the variable made inside startTransaction()
.
Every time in a random script I call the startTransaction()
and with the variable $transaction_called I check if the function is called.
This is what I have, but I don't know how to fix it.
class Foo
{
protected $_transaction_called = '0';
public function transactionCalled()
{
// code ...
$this->_transaction_called = '1';
}
public function startTransaction()
{
if(!$this->_transaction_called === '1') {
$this->transactionCalled();
mysql_query("START TRANSACTION", $db);
}else{
//do nothing, transaction already started
}
}
public function transactionStopped()
{
// code ...
$this->_transaction_called = '0';
}
public function stopTransaction()
{
if(!$this->_transaction_called === '1') {
$this->transactionStopped();
if($transaction_error==true){
mysql_query("ROLLBACK", $db);
}else{
mysql_query("COMMIT", $db);
}
}
}
}
//some random script:
$transaction_error=false;
startTransaction();
$query_1 = "UPDATE x1 SET X1='1' WHERE X1='x'";
$result_1 = mysql_query($query_1, $db);
if(!$result_1){$transaction_error=true;}
//query's, query's and more query's
stopTransaction();