Home  >  Article  >  Backend Development  >  PHP connects to mysql database and writes data_PHP tutorial

PHP connects to mysql database and writes data_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:05:571120browse

Friends who use PHP programs know that PHP and MySQL are a very good pair. We often use it for WEB development. Now I will introduce the simplest PHP method to connect to the MySQL database and write data. Friends in need can refer to.

Principle steps

1. PHP uses mysql_connect to connect to the mysql database

2. Open the MySQL database

3. Accept page data and enter it into the specified table with PHP

4. Then use mysql_query to execute and save the sql into the database.


Connecting to mysql database is super simple as follows

The code is as follows
 代码如下 复制代码
mysql_connect("localhost","root","");//连接MySQL
mysql_select_db("hello");//选择数据库
?>
Copy code

mysql_connect("localhost","root","");//Connect MySQL

mysql_select_db("hello");//Select database
代码如下 复制代码

require_once("conn.php");//引用数据库链接文件

$uname = $_GET['n'];//GET方法为URL参数传递
$psw = $_GET['p'];
$psw=md5($psw);//直接使用MD5加密

$sql = "insert into members(username,password) values ('$uname','$psw')";
mysql_query($sql);//借SQL语句插入数据

mysql_close();//关闭MySQL连接
echo "成功录入数据";
?>

?>

 代码如下 复制代码

$uname = $_GET['n'];//GET方法为URL参数传递
$psw=md5($_GET['p']);//直接使用MD5加密
$conn=mysql_connect("localhost","root","");//连接MySQL
$sql = "insert into members set username='uname',password='$psw'";
mysql_db_query("hello",$sql,$conn);//借SQL语句插入数据
?>

Example 1

The code is as follows
Copy code
require_once("conn.php");//Reference database link file $uname = $_GET['n'];//GET method passes URL parameters $psw = $_GET['p']; $psw=md5($psw);//Use MD5 encryption directly $sql = "insert into members(username,password) values ​​('$uname','$psw')"; mysql_query($sql);//Insert data using SQL statements mysql_close();//Close the MySQL connection

echo "Data entered successfully"; ?>
Combined with simpler
The code is as follows Copy code
$uname = $_GET['n'];//GET method passes URL parameters <🎜> $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);//Insert data using SQL statements<🎜> ?> http://www.bkjia.com/PHPjc/630728.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630728.htmlTechArticleFriends who use php programs all know that php and mysql are a very good pair. We often use it for WEB Development, let me introduce the simplest PHP method to connect to mysql database and write data, if necessary...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn