我有一个用户注册程序,步骤如下:
如果用户发送注册表单,则:
1. step: run a QUERY
2. step: run another QUERY
3. make a directory1 on the server
4. make a sub directory2 into the directory1
5. make another sub directory3 into the directory1
6. upload an image into the sub directory2
所以这很好用。但是我担心如果这个程序运行时发生了什么事情,所以如果程序在4.步骤中断,那么我的表格中有一些不必要的行,因为步骤1。 2。
所以我认为我必须使用mysql事务处理,但我不知道如何。我想这样想:
START TRANSACTION (what is the exact format?)
1. step: run a QUERY
2. step: run another QUERY
3. make a directory1 on the server
4. make a sub directory2 into the directory1
5. make another sub directory3 into the directory1
6. upload an image into the sub directory2
COMMIT (if all step ok. but how can i check this?)
ROLLBACK (if something wrong; but how can i check this?)
I this transaction handle not handle the FILE transactions, so i ROLLBACK function is called, then i have to delete manually the directorys if created already?!
Sorry, two more question:
如果程序中断,例如在步骤2中,并且调用ROLLBACK,则sciprt将停止或从结束开始?或者我需要编写一个类似这样的回调函数:(这是我必须做的吗?)
function begin() { @mysql_query("BEGIN");}
function commit(){ @mysql_query("COMMIT");}
function rollback()
{
@mysql_query("ROLLBACK");
echo "oops";
exit();
}
$inster_query="insert into....
begin();
$result_insert = @mysql_query($inster_query);
if(!$result_insert)
{
rollback();
}
$update_query="update....
$result_up = @mysql_query($update_query);
if(!$result_up)
{
rollback();
}
.
.
commit();
我是如何测试它的工作与否? 谢谢。
答案 0 :(得分:1)
START TRANSACTION - 参见手册
从开发角度来看,您需要创建撤消堆栈并推送能够撤消工作的对象。如果出现故障,您需要 - >执行()执行整个撤消堆栈。我想你知道如何检查目录创建/文件上传是否失败?
答案 1 :(得分:1)
您要执行的步骤是:
1. step: run a QUERY
2. step: run another QUERY
3. make a directory1 on the server
4. make a sub directory2 into the directory1
5. make another sub directory3 into the directory1
6. upload an image into the sub directory2
首先,我想讨论一下这些查询...意味着你可以如何发送多个查询,并在一切正常后保存它们。
假设您的数据库名称为'database'
,表名为'table_1'
,'table_2'
,'table_3'
...
你想做类似下面的事情:
step 1: insert a row in table_1
step 2: update a column in table_2 where id is 1
step 3: insert another row in table_3
但你需要完成所有这三个步骤或者没有一个步骤...... 在这种情况下,你可以使用这个PHP代码......
// first connect and select to your mysql database...
// before sending any queries (INSERT, UPDATE etc.) we have send this command else it will not work...
mysql_query("START TRANSACTION;");
/* this command will say to the mysql that it only have to calculate the queries that we are
sending is right or have problems... if everything is right, it will remember the query and
after sending the confirmation it will trigger the queries... besides if the query has any
problem then it will return FALSE only... moreover for right query mysql return TRUE...*/
// now I will send the queries and save the return values to different variables...
$query1 = "INSERT INTO `database`.`table_1` (col1, col2) VALUES ('value1', 'value2');";
$query1 = mysql_query($query1);
$query2 = "UPDATE `database`.`table_2` SET col1='value1', col1='value1' WHERE id='1';";
$query2 = mysql_query($query2);
$query3 = "INSERT INTO `database`.`table_3` (col1, col2) VALUES ('value1', 'value2');";
$query3 = mysql_query($query3);
// now if anything is wrong, I will get FALSE as return value for each wrong query...
// if all of them are TRUE, we will send the confirmation for save them...
// else we will say to forget whatever we send before...
if($query1 && $query2 && $query3){
// send confirmation
mysql_query('COMMIT;');
/* NOTE: after you send COMMIT you can't ROLLBACK any query or data...*/
} else {
// order to forget whatever we send before
mysql_query('ROLLBACK');
/* NOTE: after you send ROLLBACK you will lose all the queries that you send
after you send START TRANSACTION to mysql...*/
}
注意:请始终牢记,您不能 ROLLBACK
所有mysql_query
...此处列出的是您可以ROLLBACK
{ {1}} ...
CREATE DATABASE
ALTER DATABASE
DROP DATABASE
CREATE TABLE
ALTER TABLE
DROP TABLE
RENAME TABLE
TRUNCATE TABLE
CREATE INDEX
DROP INDEX
CREATE EVENT
DROP EVENT
CREATE FUNCTION
DROP FUNCTION
CREATE PROCEDURE
DROP PROCEDURE
我认为你得到了你想要做的答案......但是这里是你项目的php代码......
// connect and select your database...
mysql_query("START TRANSACTION;");
/* step 1 */
$query1 = "your query";
$query1 = mysql_query($query1);
/* step 2 */
/* you can also use multiple line to build your query */
$query2 = "your query part 1";
$query2 .= "your query part 2";
$query2 .= "your query part 3";
$query2 = mysql_query($query2);
/* step 3 */
mkdir(' the path you want to create ', 0700);
/* here '0700' is the mode (permission or chmod)...
if you don't know about it or want to know about it check the link...
www.php.net/manual/en/function.chmod.php */
/* step 4 */
mkdir('<the path you write in step 3>/<the folder name that you want to create in step 4>', 0700);
/* step 5 */
mkdir('<the path you write in step 3>/<the folder name that you want to create in step 5>', 0700);
/* step 6 */
/* I am really sorry to say that I have on knowledge about
the code that need to write for uploading a image or any file...
so I am unable to help you for this step */
/* now we need to check is everything is right or not...
we will create a variable '$rollback' and
set its value as FALSE... if we get any problem we will set it to TRUE*/
$rollback = FALSE;
// first we will check the directory !important
/* use array and foreach to do that... it will make it easy and
if you got more directory later, you just have to add it to the array... nothing more */
$dir = array();
$dir[] = 'your directory 1';
$dir[] = 'your directory 2';
$dir[] = 'your directory 3';
foreach ($dir as & $single_dir) {
if(file_exists($single_dir) == FALSE){
$rollback = TRUE;
}
}
// now if the $rollback is false, we unable to create any directory...
if($rollback==FALSE){
foreach ($dir as & $single_dir){
if(file_exists($single_dir)){
rmdir($single_dir)
/* friend though I use rmdir to remove the directory but
it has many limitation... watch this link...
www.php.net/manual/en/function.rmdir.php */
}
}
}
// lets check the image that we upload
if(file_exists('image directory') == FALSE){
$rollback = TRUE;
} else {
if($rollback == TRUE){
unlink('image directory'); /* don't use rmdir for deleting file */
}
}
// now time to check the queries...
if($query1 && $query2 $$ $rollback){
mysql_query('COMMIT;');
} else {
mysql_query('ROLLBACK;');
}
朋友们,我希望我的回答对你和那些有同样问题的人都非常有帮助......如果你在我的回答中发现任何错误,请善意原谅我,如果可以,请更正它对我来说...而且如果你认为我的答案对别人有帮助,请不要忘记投票给这个答案...谢谢你......朋友......