本文實例講述了PHP通過插入mysql數據來實現多機互鎖的方法,分享給大家供大家參考。具體實現方法如下:
在執行流程之前可以加一般鎖,shell枷鎖函數如下,如果成功則會返回0,否則返回非0值:
復制代碼 代碼如下:function get_lock()
{
local dataId="${1}"
local dataDate="${2}"
local sql="insert intot_trans_lock
(dataId, dataDate) values('${dataId}', '${dataDate}');"
echo ${sql} | ${DB_PUBLIC}
return $?
}
在執行失敗或者結束的時候釋放鎖
復制代碼 代碼如下:function free_lock()
{
local dataId="${1}"
local dataDate="${2}"
local status="${3}"
local sql="delete from t_trans_lock
where dataId='${dataId}' and dataDate='${dataDate}';"
echo ${sql} | ${DB_PUBLIC}
if [ $? -ne 0 ]; then
write_log ${dataId} "free lock failed"
fi
return ${status}
}
希望本文所述對大家的PHP+MySQL程序設計有所幫助。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/...al.dtd">
<html xmlns="www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<body>
<?php
if(isset($_POST['tj']))//若點擊了提交
{
$conn=mysql_connect("localhost","username","password");
mysql_select_db("eastses",$conn);
mysql_query("set names utf8",$conn);
date_default_timezone_set("asia/chongqing");
$sql = "INSERT INTO `eastses`.`classmate` (`id`, `name`, `nickname`, `birthday`, `home`, `blood`, `qq`, `weibo`, `email`, `phone`, `hobby`, `food`, `sentence`, `gift`, `lover`, `keenon`, `unforgettable`, `wanttobecome`, `ideal`, `other`) VALUES ('".$_POST['id']."', '".$_POST['name']."', '".$_POST['nickname']."', '".$_POST['birthday']."', '".$_POST['home']."', '".$_POST['blood']."', '".$_POST['qq']......余下全文>>
注冊頁面:reg.html
<form action="reg.php" method="POST">
<table>
<tr><td>用戶名:</td><td><input type="username" size="20"></td></tr>
<tr><td>密碼:</td><td><input type="userpass" size="20"></td></tr>
<tr><td>確認密碼:</td><td><input type="ruserpass" size="20"></td></tr>
<tr><td>郵箱:</td><td><input type="email" size="50"></td></tr>
<tr><td>電話:</td><td><input type="telphone" size="20"></td></tr>
<tr><td><input type="Submit" value="注冊"></td></tr>
</table>
</form>
接收頁面:reg.php
<%php
$db = mysql_connect("localhost", "root", "12345");
mysql_select_db("dataname", $db);
mysql_query("insert into tablename(username, userpass, email, telphone) values('$_POST[username]', '$_POST[userpass]', '$_POST[email]', '$_POST[telphone]')");
echo "注冊成功";
%>