mysql教程觸發器資料庫教程同步資料方法
<script language="網頁特效">
function res(){
document.getElementById("tab2").value = "pre_"+document.getElementById("tab1").value;
}
</script>
</head>
<body>
<form action="#" method="post" name="form1" target="_blank" id="form1">
資料庫名
<input name="dbTbaleName" type="text" id="dbTbaleName" value="dbname" />
<br>
驅動表
<input name="tab1" type="text" id="tab1" /> <br>
被觸發的表
<input name="tab2" type="text" id="tab2" /><br>
<input type="submit" name="Submit" value="提交" /><br>
</form>
<?php教程
/**
* -------------------文檔注釋------------------------------------------
* 作者*達達*QQ 85089479*email qianshuei@163.com*
* 建立時間*2006-012-*
* 功能描述*資料同步觸發器產生工具*
* 最後修改時間*2006-12-*
* -------------------------------------------------------------------
*/
$dbName = "localhost"; //主機名稱
$dbUserName = "root"; // 使用者名稱
$dbpassword = "admin"; //密碼
$dbTbaleName = $_POST['dbTbaleName']; //資料庫名
$tab1 = $_POST['tab1']; //執行的表
$tab2 = $_POST['tab2']; //被觸發的表
$conn = mysql_connect($dbName, $dbUserName, $dbpassword) or die("請檢查你的主機名稱資料庫使用者名稱和密碼");
mysql_select_db($dbTbaleName, $conn) or die("資料庫還沒有串連");
mysql_select_db("$dbTbaleName");
$result = mysql_query("select * from $tab1")//得到表查詢的結果
or die("Query failed: " . mysql_error());
echo("<br>");
$i = 1;//這裡是為了替換最後一個列名的,符號所以=1 <>0
echo "create trigger " . $tab1 . "_insert BEFORE INSERT on $tab1<br>";
echo "for each row BEGIN<br>";
echo "INSERT INTO $tab2 SET<br>";
while ($i < mysql_num_fields($result)){//列的數量的迴圈
$meta = mysql_fetch_field($result);//得到一個關於列的資訊的對角
if (!$meta){
echo "No information available<br />n";
}
//下面輸出的是觸發器程式列資訊
echo "<pre>
`$meta->name`=NEW.$meta->name,
</pre>";
$i++;
}
$meta = mysql_fetch_field($result);
echo "<pre>
`$meta->name`=NEW.$meta->name;
</pre>";
mysql_free_result($result);
echo("END;");
// -----------------------------------------------------------------
$result = mysql_query("select * from $tab1")
or die("Query failed: " . mysql_error());
$i = 1;
if ($i < mysql_num_fields($result)){
$meta = mysql_fetch_field($result);
// -----------------------------------------------------------------
/**
* create trigger `tri_del` BEFORE DELETE on `aaa`
* for each row BEGIN
* DELETE FROM bbb WHERE id =OLD.id;
*/
echo("<br>");
echo "create trigger " . $tab1 . "_del BEFORE DELETE on $tab1<br>";
echo "for each row BEGIN<br>";
echo "DELETE FROM $tab2 WHERE $meta->name =OLD.$meta->name;<br>";
echo("END;");
}
// -----------------------------------------------------------------
echo("<br>");
/**
* create trigger `up_date` BEFORE UPDATE on `aaa`
* for each row BEGIN
* UPDATE bbb set a1 = new.a1 , a2=new.a2 where id = new.id;
* END;
*/
$result = mysql_query("select * from $tab1")
or die("Query failed: " . mysql_error());
/**
* get column metadata
*/
$i = 1;
echo "create trigger " . $tab1 . "_update BEFORE UPDATE on $tab1<br>";
echo "for each row BEGIN<br>";
echo "UPDATE $tab2 SET<br>";
while ($i < mysql_num_fields($result)){
$meta = mysql_fetch_field($result);
if (!$meta){
echo "No information available<br />n";
}
echo "<pre>
`$meta->name`=NEW.$meta->name,
</pre>";
$i++;
}
$meta = mysql_fetch_field($result);
echo "<pre>
`$meta->name`=NEW.$meta->name
</pre>";
mysql_free_result($result);
// -----------------------------
$result = mysql_query("select * from $tab1")
or die("Query failed: " . mysql_error());
$i = 1;
if ($i < mysql_num_fields($result)){
$meta = mysql_fetch_field($result);
echo("where $meta->name = new.$meta->name;<br>");
echo("END;");
}
?>