MySQL trigger database synchronization data method

Source: Internet
Author: User
Tags mysql tutorial php tutorial name database

MySQL Tutorial triggers database tutorial Synchronizing data methods

<script language= "Web Effects" >

function res () {
document.getElementById ("TaB2"). Value = "Pre_" +document.getelementbyid ("Tab1"). Value;
}
</script>

<body>
<form action= "#" method= "Post" Name= "Form1" target= "_blank" id= "Form1" >
Database name
<input name= "Dbtbalename" type= "text" id= "Dbtbalename" value= "dbname"/>
<br>
Driver table
<input name= "Tab1" type= "text" id= "Tab1"/> <br>
The table being triggered
<input name= "tab2" type= "text" id= "TAB2"/><br>
<input type= "Submit" name= "Submission" value= "submitted"/><br>
</form>
<?php Tutorial
/**
*-------------------Documentation Comments------------------------------------------
* The author * tatsu *qq 85089479*email qianshuei@163.com*
* Create Time *2006-012-*
* Function Description * Data synchronization trigger Generation tool *
* Last Modified Time *2006-12-*
* -------------------------------------------------------------------
*/
$dbName = "localhost"; Host Name
$dbUserName = "root"; User name
$dbpassword = "admin"; Password
$dbTbaleName = $_post[' dbtbalename ']; Database name
$tab 1 = $_post[' tab1 ']; The table executed
$tab 2 = $_post[' tab2 ']; The table being triggered
$conn = mysql_connect ($dbName, $dbUserName, $dbpassword) or Die ("Please check your host name database username and password");
mysql_select_db ($dbTbaleName, $conn) or Die ("Database not yet connected");
mysql_select_db ("$dbTbaleName");
$result = mysql_query ("SELECT * from $tab 1")//Get the result of a table query
Or Die ("Query failed:".) Mysql_error ());
Echo ("<br>");
$i = 1;//Here is the symbol for replacing the last column name, so =1 <>0
echo "CREATE TRIGGER". $tab 1. "_insert before insert on $tab 1<br>";
echo "For each row begin<br>";
echo "INSERT into $tab 2 set<br>";
while ($i < Mysql_num_fields ($result)) {//column number of loops

$meta = Mysql_fetch_field ($result);//Get a diagonal of the information about the column
if (! $meta) {
echo "No information available<br/>n";
}
The following output is the trigger program column information
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 $tab 1")
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". $tab 1. "_del before DELETE on $tab 1<br>";
echo "For each row begin<br>";

echo "DELETE from $tab 2 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 $tab 1")
Or Die ("Query failed:".) Mysql_error ());
/**
* Get column metadata
*/
$i = 1;
echo "CREATE TRIGGER". $tab 1. "_update before update on $tab 1<br>";
echo "For each row begin<br>";
echo "UPDATE $tab 2 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 $tab 1")
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;");

}
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.