PHP inserts mysql Data to implement multi-host mutual lock instances and mysql instances

Source: Internet
Author: User

PHP inserts mysql Data to implement multi-host mutual lock instances and mysql instances

This article describes how PHP inserts mysql Data to implement multi-host lock. The specific implementation method is as follows:

You can add a general lock before executing the process. The shell yoke function is as follows. If it succeeds, 0 is returned; otherwise, a non-0 value is returned:

Copy codeThe Code is as follows: function get_lock ()
{
Local dataId = "$ {1 }"
Local dataDate = "$ {2 }"

Local SQL = "insert into t_trans_lock
(DataId, dataDate) values ('$ {dataId}', '$ {dataDate }');"
Echo $ {SQL} | $ {DB_PUBLIC}

Return $?
}

Release the lock when execution fails or ends.
Copy codeThe Code is as follows: 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}
}

I hope this article will help you design the PHP + MySQL program.


A simple example of database insertion/reading in PHP

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
</Head>

<Body>
<? Php

If (isset ($ _ POST ['tj ']) // if you click Submit
{

$ 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 'asts '. 'classmat' ('id', 'name', 'nickname', 'birthday', 'home', 'blood ', 'qq', 'weibo', 'email ', 'Phone ', 'hoby', 'food', 'sentence', 'giput', 'lover ', 'keenon', 'Unforgettable', 'wanttobecome', 'ideal ', 'other') VALUES ('". $ _ POST ['id']. "','". $ _ POST ['name']. "','". $ _ POST ['nickname']. "','". $ _ POST ['birthday']. "','". $ _ POST ['home']. "','". $ _ POST ['blood ']. "','". $ _ POST ['qq'] ...... remaining full text>

Php implements a simple registration page and inserts registration information into the mysql database.

Registration page: reg.html

<Form action = "reg. php" method = "POST">
<Table>
<Tr> <td> User name: </td> <input type = "username" size = "20"> </td> </tr>
<Tr> <td> password: </td> <input type = "userpass" size = "20"> </td> </tr>
<Tr> <td> Confirm Password: </td> <input type = "ruserpass" size = "20"> </td> </tr>
<Tr> <td> email: </td> <input type = "email" size = "50"> </td> </tr>
<Tr> <td> Tel: </td> <input type = "telphone" size = "20"> </td> </tr>
<Tr> <td> <input type = "Submit" value = "register"> </td> </tr>
</Table>
</Form>

Receiving page: 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 "registration successful ";
%>

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.