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