How to save data into database in php

Release: 2023-02-27 22:38:01
Original
9679 people have browsed it

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;);
?>
Copy after login

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语句插入数据

?>
Copy after login

Close the database

<?php
mysql_close();
?>
Copy after login

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语句插入数据
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template