PHP connects to MySQL database and writes data

Source: Internet
Author: User

Principle Steps

1,php use mysql_connect to connect with MySQL database

2, open MySQL database

3, accept the page data, PHP input to the specified table

4, and then use Mysql_query to execute and save the SQL to the database.


Connection MySQL database super simple as follows

The code is as follows Copy Code
<?php
mysql_connect ("localhost", "root", "");//Connect MySQL
mysql_select_db ("Hello");//Select Database
?>

Example 1

The code is as follows Copy Code

<?php
Require_once ("conn.php");//referencing Database link file

$uname = $_get[' n '];//get method is passed to the URL parameter
$PSW = $_get[' P '];
$PSW =md5 ($PSW);//Direct use of MD5 encryption

$sql = "INSERT into the members (Username,password) VALUES (' $uname ', ' $psw ')";
mysql_query ($sql);//Borrow SQL statement to insert data

Mysql_close ()//close MySQL connection
echo "Successful data entry";
?>

It's a much simpler combination.

  code is as follows copy code

.
$ uname = $_get[' n '];//get method passes
$PSW =md5 ($_get[' P ']) for the URL parameter;//Direct use MD5 encryption
$conn =mysql_connect ("localhost", "root" , "");//connect mysql
$sql = INSERT INTO members set Username= ' uname ', password= ' $PSW ';
mysql_db_query ("Hello", $sql, $conn);//Borrow 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.