Home >Backend Development >PHP Tutorial >merry lonely christmas PHP writes MySQL data implementation code

merry lonely christmas PHP writes MySQL data implementation code

WBOY
WBOYOriginal
2016-07-29 08:40:011222browse

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 be used directly A database link file is enough: conn.php

Copy the code The code is as follows:


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


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

Copy the code The code is as follows:

< ;?php
require_once("conn.php");//Reference the database link file
$uname = $_GET['n'];//The GET method passes URL parameters
$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
Insert new data "cnbruce" into the username field and "i0514" into the password field in the members table of the MySQL database hello

The above introduces the merry lonely christmas PHP writing MySQL data implementation code, including the content of merry lonely christmas. I hope it will be helpful to friends who are interested in PHP tutorials.

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