当前位置:网站首页>PHP realizes data interaction with MySQL
PHP realizes data interaction with MySQL
2022-07-27 08:02:00 【Shenzhi Kalan Temple】
PHP Is a powerful server-side scripting language for creating dynamic interactive sites
MySQL Is the most popular relational database management system , Especially WEB Application is very important
So how do these two interact , The method is as follows :

The following is a brief description of a simple hotel module business control on and off php call mysql
PHP call MySQL Insert data into
Add hotel information
<?php
/*
* The front end can call this php Script adds hotel room information , The corresponding table is dwd_hotel_module_baseinfo
s_hotel_id int comment ' The hotel ID' ,
s_hotel_name varchar(50) comment ' Hotel name ',
*/
// These variables are the values passed in by the front end
$s_hotel_id = $_POST["s_hotel_id"];
$s_hotel_name = $_POST["s_hotel_name"];
// Fill in the database information here
$conn=mysqli_connect("localhost","root","passwd1","database1");
// Detection connection
if (mysqli_connect_errno())
{
echo " The connection fails : " . mysqli_connect_error();
}
// The integer type is ".$number." The character type is '".$string."'
$sql = "INSERT INTO dwd_hotel_module_baseinfo(s_hotel_id,s_hotel_name)
VALUES (".$s_hotel_id.",'".$s_hotel_name."')";
if ($conn->query($sql) === TRUE) {
echo " New record inserted successfully ";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>PHP call MySQL Data update
Control power on and off by changing the field value
<?php
/*
* The front end can call this php Script to control power on ,ads_module_onoff_switch,
s_module_id varchar(20) primary key comment ' modular ID',
n_status bigint comment ' Power on switch control 0 Delegate energized ,1 It means no electricity '
*/
// These variables are the values passed in by the front end
$s_module_id = $_POST["s_module_id"];
$n_status = $_POST["n_status"];
// Fill in the database information here
$conn=mysqli_connect("localhost","root","passwd1","database1");
// Detection connection
if (mysqli_connect_errno())
{
echo " The connection fails : " . mysqli_connect_error();
}
mysqli_query($conn,"UPDATE ads_module_onoff_switch SET n_status = ".$n_status."
WHERE s_module_id= ".$s_module_id." ");
mysqli_close($conn);
?>PHP call MySQL Return the data (JSON type )
php call mysql Data and return json Data to front end
<?php
/*
* The front end can call this php Script to get the power on status of the module , The duration of power on ,dws_hotel_module_day_status,
s_module_id varchar(20) primary key comment ' modular ID',
n_status int comment ' adopt ads_module_onoff_switch relation ',
n_eleuse_long double comment ' The duration of power on , when ',
n_date int comment ' Date of the day '
*/
// These variables are the values passed in by the front end
$s_module_id = $_POST["s_module_id"];
$n_date = date('Ymd', time());
// Fill in the database information here
$conn=mysqli_connect("localhost","root","passwd1","database1");
// Detection connection
if (mysqli_connect_errno()) {
echo " The connection fails : " . mysqli_connect_error();
}
$result1 = mysqli_query($conn, "SELECT case when n_status = 1 then ' Electrify ' else ' power failure ' end as n_status FROM ads_module_onoff_switch
where s_module_id = '".$s_module_id."' ");
while ($row = mysqli_fetch_array($result1)) {
$n_status = $row['n_status'];
}
$result2 = mysqli_query($conn, "SELECT round(sum(n_eleconn_long)/3600,2) as long1 FROM table1
where s_module_id = '".$s_module_id."' and FROM_UNIXTIME(n_eledisconn_time,'%Y%m%d')= '".$n_date."'");
while ($row = mysqli_fetch_array($result2)) {
$n_eleuse_long = $row['long1'];
}
//header('Content-Type:application/json; charset=utf-8');
$arr = array('s_module_id'=>$s_module_id,'n_status'=>$n_ele_status,'n_eleuse_long'=>$n_eleuse_long,'n_date'=>$n_date);
# return json Data is used by the front end
exit(json_encode($arr,JSON_UNESCAPED_UNICODE));
$conn->close();
?>
I like you , It's my exclusive memory
边栏推荐
- Convert objects to key value pairs
- Bash: sudo: command not found in container
- C language: random generated number + insertion sort
- DEMO:PA30 银行国家码默认CN 增强
- 一段平平无奇的秋招经历
- Promise details
- [day42 literature intensive reading] a Bayesian model of perfect head centered velocity during smooth pursuit eye movement
- Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
- 【小程序】如何获取微信小程序代码上传密钥?
- Kalibr calibration realsensed435i -- multi camera calibration
猜你喜欢

一文速览EMNLP 2020中的Transformer量化论文

大家节日快乐哈

Demo submit a program and obtain ALV data of the program

What about idea Chinese garbled code

【Day42 文献精读】A Bayesian Model of Perceived Head-Centered Velocity during Smooth Pursuit Eye Movement

C#委托的使用案例

Zero training platform course-1. SQL injection Foundation

Install tensorflow
![[ten thousand words long article] thoroughly understand load balancing, and have a technical interview with Alibaba Daniel](/img/fc/1ee8b77d675e34da2eb8574592c489.png)
[ten thousand words long article] thoroughly understand load balancing, and have a technical interview with Alibaba Daniel

MCU multi-level menu
随机推荐
【已解决】单点登录成功SSO转发,转发URL中带参数导致报错There was an unexpected error (type=Internal Server Error, status=500)
Combined use of C WinForm form form event and delegate
End of year summary
Install tensorflow
How to analyze and locate problems in 60 seconds?
[stonedb class] introductory lesson 1: popular science of database knowledge
Demo submit a program and obtain ALV data of the program
[golang] golang develops wechat official account web page authorization function
一段平平无奇的秋招经历
物联网工业级UART串口转WiFi转有线网口转以太网网关WiFi模块选型
代码接口自动化的有点
【小程序】如何获取微信小程序代码上传密钥?
C#委托的使用案例
Applet payment management - new payment docking process
C语言:随机生成数+插入排序
Zero training platform course-1. SQL injection Foundation
Methods of server network testing
实用的新药研发项目管理平台
Use of NPM
SETTA 2020 国际学术会议即将召开,欢迎大家参加!