Home  >  Article  >  Backend Development  >  How to save data into database in php

How to save data into database in php

尚
Original
2019-10-29 14:41:479676browse

How to save data into database in php

To write data to a MYSQL database through PHP, you basically have to go through three steps:

  • Connect to the database

  • Write data to the database

  • Close the connection

Connect to the database code:

<?php
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PWD","");
define("DB_NAME","wordpress4");
$conn = @mysql_connect(DB_HOST,DB_USER,DB_PWD) or die("连接服务器出错:".mysql_error());
@mysql_select_db(DB_NAME) or die("连接数据库出错:".mysql_error());
@mysql_query(&#39;SET NAMES UTF8&#39;);
?>

Write Input data into the database:

<?php
$sql = "insert into wp_kaijiang(timer,number,datar) values (&#39;$timer&#39;,&#39;$number&#39;,&#39;$datar&#39;)";
mysql_query($sql);//借SQL语句插入数据

?>

Close the database

<?php
mysql_close();
?>

By combining the three pieces of code, the purpose of PHP writing data to the MYSQL database can be achieved. The code is as follows:

<?php
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PWD","");
define("DB_NAME","wordpress4");
$conn = @mysql_connect(DB_HOST,DB_USER,DB_PWD) or die("连接服务器出错:".mysql_error());
@mysql_select_db(DB_NAME) or die("连接数据库出错:".mysql_error());
@mysql_query(&#39;SET NAMES UTF8&#39;);

$sql = "insert into wp_kaijiang(timer,number,datar) values (&#39;$timer&#39;,&#39;$number&#39;,&#39;$datar&#39;)";

 mysql_query($sql);//借SQL语句插入数据
?>

Recommended: php server

The above is the detailed content of How to save data into database in php. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:Where is the php log?Next article:Where is the php log?