Home > Article > Backend Development > 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('SET NAMES UTF8'); ?>
Write Input data into the database:
<?php $sql = "insert into wp_kaijiang(timer,number,datar) values ('$timer','$number','$datar')"; 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('SET NAMES UTF8'); $sql = "insert into wp_kaijiang(timer,number,datar) values ('$timer','$number','$datar')"; 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!