5

Below are my queries

 $db= new mysqli('localhost','user','passcode','dbname');
try {
// First of all, let's begin a transaction
$db->beginTransaction();

// A set of queries; if one fails, an exception should be thrown
$query1="insert into table1(id,name) values('1','Dan')";
$query2="insert into table2(id,name) values('2','Anaba')";

// If we arrive here, it means that no exception was thrown
// i.e. no query has failed, and we can commit the transaction
$db->commit();

} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
$db->rollback();
}

Please I am trying to implement transaction in mysql queries through php, I would like to fail all queries when one of the queries fail. The above code throws an error("Fatal error: Call to undefined method mysqli::beginTransaction()").Please is there any better solution or idea to solve the problem.Thanks for your help

4
  • This is unrelated to the transactions - but where are you actually running these queries? The code you've shown is just assigning them to strings. Commented Jul 12, 2013 at 22:54
  • I am running these queries in a browser through a php script. Commented Jul 12, 2013 at 23:11
  • I mean - the code you show doesn't actually execute the queries. Commented Jul 12, 2013 at 23:12
  • Yes please, it doesn't execute the queries but rather it shows the error message aforementioned. Commented Jul 12, 2013 at 23:13

3 Answers 3

10
$db->begin_transaction();

not

$db->beginTransaction();

http://www.php.net/manual/en/mysqli.begin-transaction.php

If earlier than PHP 5.5, then use

$db->autocommit(true);

instead

Sign up to request clarification or add additional context in comments.

8 Comments

It shouldn't be the same error, since the error message references beginTransaction, which should no longer be in your code. Please include the exact new error message.
Please this is the error it gives after updating it with begin_transaction(). " Fatal error: Call to undefined method mysqli::begin_transaction() in /var/www/books.php on line 19"
Please sorry for the wrong information, I just confirmed from my server, It is rather PHP 5 version.Thanks for your understanding
Please after applying the $db->autocommit(true), it throws no error but the first query works while the second query fails.
Both the autocommit(false) and autocommit(right) give the same result.
|
2

According to the documentation, begin_transaction() is new in PHP 5.5. (just released a couple weeks ago)

If you're getting an error saying that it doesn't exist, it probably means you're using an older version of PHP.

If you think you are running PHP 5.5, please verify your PHP version by running this earlier in your same script:

echo(phpversion());

Sometimes there's multiple versions of PHP present on a machine, and your script may not actually be executing in the version that you think it is.

3 Comments

Please I am using PHP 5 version.
PHP 5 is not necessarily PHP 5.5. PHP 5.5 was just released a couple weeks ago.
Please I have corrected my earlier comment, I was sorry for the wrong information,I just verified from my server and it was PHP 5 not PHP 5.5.Thanks for your understanding.
0

You could implement a $count, something like that:

$err_count = 0;
$query1 = "insert into table1(id,name) values('1','Dan')";
if(!$query1) 
    $err_count++;

if($err_count>0) 
    throw new Exception("error msg");

1 Comment

Please, it is not responding at all.Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.