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