使用php无法使用mysql的事务 [英] Transactions not working for mysql using php

查看:129
本文介绍了使用php无法使用mysql的事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hiya all,


经过我1-2周的研究后,我终于找到了一种更加一致的方法来解决php中的mysql查询,即交易。我有两个选项可以在多个表中插入数据::


1)通过程序

2)通过交易


当然我更喜欢2路即交易。我通过罗纳德给我链接的所有手册。但是阅读了许多论坛和教程,我再次登陆这里只是为了知道为什么我的代码不会按照我想要的方式输出。看看我的代码:

[PHP]

<?php

试试{

$ dbh = new PDO(''mysql:host = localhost; port = 3306; dbname = test'',''root'','''',array(PDO :: ATTR_PERSISTENT => true));


$ dbh-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);

$ dbh-> beginTransaction();

$ dbh-> exec(" update pop set age = 1 where id = 1");

$ dbh-> rollBack();

} catch(PDOException $ e){

print" Error!:" 。 $ e-> getMessage()。 "< br />";

die();

}

?> [/ PHP]


它不会回滚,如代码中所写,它不应该影响表pop的age列,因为它符合$ dbh-> rollback()函数,但它会自动提交。


请帮帮我..

我希望autoCommit的东西能用php关掉。


干杯!

Realin!

解决方案

dbh = new PDO(''mysql:host = localhost; port = 3306; dbname = test'', ''root'','''',array(PDO :: ATTR_PERSISTENT => true));


dbh-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);


dbh-> beginTransaction();


Hiya all,

after my 1-2 weeks research i finally found a more consistent way to fire mysql queries in php i.e. transactions. I have two options to insert data in multiple tables ::

1) through procedures
2) through transactions

Of course i am going to prefer is the 2 way i.e. transactions. I went thru all the manuals of which Ronald gave me the links. But reading many forums and tutorials, i land up here again just to know why my code wont output the way i want. Just have a look at my code :
[PHP]
<?php
try {
$dbh = new PDO(''mysql:host=localhost;port=3306;dbname=test'', ''root'', '''', array( PDO::ATTR_PERSISTENT => true));

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->beginTransaction();
$dbh->exec("update pop set age=1 where id=1");
$dbh->rollBack();
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>[/PHP]

It wont rollback, as written in code, it should not affect the age column of the table pop, because it meets the $dbh->rollback() function, but it will automaticaly commit.

please help me ..
I want the autoCommit thing to turn off using php.

cheers !!
Realin !

解决方案

dbh = new PDO(''mysql:host=localhost;port=3306;dbname=test'', ''root'', '''', array( PDO::ATTR_PERSISTENT => true));


dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


dbh->beginTransaction();


这篇关于使用php无法使用mysql的事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆