PHP write Mysql Data Implementation Code

Source: Internet
Author: User

PHP writes data to the MySQL database in three steps:
1. Establish a connection between PHP and MySQL
2. Open the MySQL database
3. Accept page data. Input PHP Data to the specified table.
Steps 1 and 2 can directly use a database link file: conn. php CopyCode The Code is as follows: <? PHP
Mysql_connect ("localhost", "root", ""); // connect to MySQL
Mysql_select_db ("hello"); // select a database
?>

Of course, the premise is that you have installed the web server, PHP, and MySQL, and created the MySQL table "cnbruce"
The three parameters of mysql_connect () are MySQL address, MySQL user name, and MySQL password.
Then, the data is transmitted through the web page, so that PHP writes data to the table specified by the MySQL database through SQL statements, such as creating a new file post. phpCopy codeThe Code is as follows: <? PHP
Require_once ("conn. php"); // reference the database link file
$ Uname = $ _ Get ['n']; // The get method is passed as a URL parameter.
$ Psw = $ _ Get ['P'];
$ Psw = MD5 ($ psw); // use MD5 encryption directly
$ SQL = "insert into members (username, password) values ('$ uname',' $ psw ')";
Mysql_query ($ SQL); // use SQL statements to insert data
Mysql_close (); // close the MySQL connection
Echo "successfully entered data ";
?>

Test page: http: // localhost/post. php? N = cnbruce & P = i0514
You can insert the new data "cnbruce" to the username field and "i0514" to the password field into the members table of MySQL database hello.

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.