PHP connect MySQL database and write data _php tutorial

Source: Internet
Author: User
Tags md5 encryption php and mysql
PHP program friends know that PHP and MySQL very good pair, we often use it to do web development, the following I will introduce the simplest PHP connection MySQL database and write data, the need for friends to refer to.

Principle Steps

1,php using mysql_connect to connect to MySQL database

2. Open MySQL Database

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

4. Then use Mysql_query to execute and save SQL to the database.


Connecting MySQL database super simple as follows

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

Example 1

The code is as follows Copy Code

Require_once ("conn.php");//reference Database link file

$uname = $_get[' n '];//get method passed for 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 MySQL connection
echo "successfully entered data";
?>

Combined with a simpler

The code is as follows Copy Code

$uname = $_get[' n '];//get method passed for URL parameter
$PSW =md5 ($_get[' P ']);//use MD5 encryption directly
$conn =mysql_connect ("localhost", "root", "");//Connect MySQL
$sql = "INSERT INTO members set Username= ' uname ', password= ' $psw '";
Mysql_db_query ("Hello", $sql, $conn);//Use SQL statements to insert data
?>

http://www.bkjia.com/PHPjc/630728.html www.bkjia.com true http://www.bkjia.com/PHPjc/630728.html techarticle PHP program friends know that PHP and MySQL is a very good pair, we often use it to do web development, let me introduce the simplest PHP connection MySQL database and write data, there is a need ...

  • 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.