Php connection to mysql database and write data _ PHP Tutorial

Source: Internet
Author: User
Php connects to the mysql database and writes data. All the friends who use the php program know that php and mysql are very good. we often use it for WEB development. next I will introduce the simplest php connection to the mysql database and write data, anyone who needs to use the php program knows that php and mysql are very good. we often use it for WEB development. next I will introduce the simplest php connection to the mysql database and write data, for more information, see.

Procedure

1. php uses mysql_connect to connect to the mysql database

2. open the MySQL database

3. accept page data. input PHP Data to the specified table.

4. execute the SQL statement and save it to the database using mysql_query.


Connecting to the mysql database is as simple as follows:

The code is as follows:
Mysql_connect ("localhost", "root", ""); // Connect to MySQL
Mysql_select_db ("hello"); // select a database
?>

Example 1

The code is as follows:

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 ";
?>

Combined with simpler

The code is as follows:

$ Uname = $ _ GET ['n']; // The GET method is passed as a URL parameter.
$ Psw = md5 ($ _ GET ['P']); // Use MD5 encryption directly
$ Conn = mysql_connect ("localhost", "root", ""); // Connect to MySQL
$ SQL = "insert into members set username = 'uname', password = '$ psw '";
Mysql_db_query ("hello", $ SQL, $ conn); // use an SQL statement to insert data.
?>

...

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.