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, 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: |
Copy code |
<? Php
Mysql_connect ("localhost", "root", ""); // connect to MySQL
Mysql_select_db ("hello"); // select a database
?>
|
Example 1
| The Code is as follows: |
Copy code |
|
<? Php
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: |
Copy code |
|
<?
$ 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.
?> |