Home >Backend Development >PHP Tutorial >PHP writes MySQL data implementation code_PHP tutorial

PHP writes MySQL data implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:46:02926browse

There are three steps for PHP to write data to the MySQL database:
1. Establish a connection between PHP and MySQL
2. Open the MySQL database
3. Accept the page data, and PHP enters it into the specified table
Steps 1 and 2 can directly use a database link file: conn.php

Copy the code The code is as follows:

mysql_connect("localhost","root","");//Connect to MySQL
mysql_select_db("hello");//Select database
?>

Of course, the premise is that the WEB server, PHP and MySQL have been installed, and the MySQL table "cnbruce" has been created
The three parameters in mysql_connect() are the MySQL address, MySQL username and MySQL password
Then pass The WEB page transfers data and lets PHP write the data into the table specified in the MySQL database through SQL statements, such as creating a new file post.php
Copy code The code is as follows:

require_once("conn.php");//Reference database link file
$uname = $_GET['n'];//The GET method is URL parameter passing
$psw = $_GET['p'];
$psw=md5($psw);//Use MD5 encryption directly
$sql = "insert into members(username,password) values ​​('$uname','$psw')";
mysql_query($sql);//Insert data using SQL statements
mysql_close();//Close the MySQL connection
echo "Successfully entered data ";
?>

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320202.htmlTechArticleThere are three steps for PHP to write data to the MySQL database: 1. Establish a connection between PHP and MySQL 2. Open MySQL Database 3, accept page data, PHP enters it into the specified table in two steps 1 and 2...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn