Mysql trigger Database Data Synchronization Method

Source: Internet
Author: User

Mysql tutorial trigger database tutorial Data Synchronization Method

<Script language = "webpage effect">

Function res (){
Document. getElementById ("tab2"). value = "pre _" + document. getElementById ("tab1"). value;
}
</Script>
</Head>

<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>
Triggered table
<Input name = "tab2" type = "text" id = "tab2"/> <br>
<Input type = "submit" name = "Submit" value = "submit"/> <br>
</Form>
<? Php tutorial
/**
* ------------------- Document comment ------------------------------------------
* Author * da * QQ 85089479 * email qianshuei@163.com *
* Creation time * 2006-012 -*
* Function Description * Data Synchronization trigger generation tool *
* Last modification time * 2006-12 -*
*-------------------------------------------------------------------
*/
$ DbName = "localhost"; // host name
$ DbUserName = "root"; // User Name
$ Dbpassword = "admin"; // Password
$ DbTbaleName = $ _ POST ['dbtbalename']; // Database Name
$ Tab1 = $ _ POST ['tab1']; // executed table
$ Tab2 = $ _ POST ['tab2']; // triggered table
$ Conn = mysql_connect ($ dbName, $ dbUserName, $ dbpassword) or die ("check your host name database username and password ");
Mysql_select_db ($ dbTbaleName, $ conn) or die ("the database is not connected ");
Mysql_select_db ("$ dbTbaleName ");
$ Result = mysql_query ("select * from $ tab1") // obtain the Table query result.
Or die ("Query failed:". mysql_error ());
Echo ("<br> ");
$ I = 1; // This is to replace the last column name, so the symbol = 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) {// a loop of the number of Columns

$ Meta = mysql_fetch_field ($ result); // obtain the diagonal corner of the column information.
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 $ 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 ;");

}
?>

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.