数据库SQL常⽤增删改查语句
1:连接数据库
conn.php⽂件
<?php
// 连接到数据库
$servername = "域名和端⼝号";
文本转html代码$username = "root";
$password = "⾃⼰的数据库密码";
$dbname = "数据库名";
// 创建连接
$conn = new mysqli($servername, $username, $password);
$conn->select_db($dbname);
// 检测连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
$conn->query('set names utf8');
}
//测试mysqli 是否可⽤
// $msg = phpinfo();
// echo $msg;
//关闭数据库
/
/ $conn->close();
2:增加数据
<?php
i nclude 'conn.php';//连接数据库的⽂件
$title = $_REQUEST['title'];
$detail = $_REQUEST['detail'];
$idea = $_REQUEST['idea'];
wq_list(⾃⼰建⽴的数据库名)
$sql = "insert into wq_list(wq_title,wq_details,wq_idea,submission_date) values('$title','$detail','$idea',now());"; //⾃⼰建⽴的数据库⾥的数据表⾥的字段内容
$ret = $conn->query($sql);
//判断是否成功
if($ret > 0) {
echo '{"msg":"success","flag":true}';
} else {
echo '{"msg":"failed","flag":false}';
}
$conn->close();
?>
3:删除数据
<?php
include 'conn.php';mysql面试题 增删改查
$id = $_REQUEST['id'];
$sql = "delete from wq_list where wq_id = $id";
$ret = $conn->query($sql);
if($ret > 0) {
this is的真人发音echo '{"msg":"success","flag":true}';
} else {
echo '{"msg":"failed","flag":false}';
render记忆}
$conn->close();
?>
贝尔摩德的秘密是什么4:修改数据update
<?php
include 'conn.php';
$title = $_REQUEST['title'];
$detail = $_REQUEST['detail'];
$idea = $_REQUEST['idea'];
$sql = " update wq_list set wq_title = $title,wq_details=$detail where wq_sid=$sid,"; //update 表名 set 字段=值,字段=值 where sid = id值
$ret = $conn->query($sql);
if($ret > 0) {
echo '{"msg":"success","flag":true}';
} else {
echo '{"msg":"failed","flag":false}';
}
$conn->close();
?>
5:查询数据
<?php
include './base/connectDb.php';
// header("Content-type: text/html; charset=utf-8");
$sql = "select * from wq_list order by wq_id ";
$result = $conn->query($sql);ascii码转换器在线转换
$ret_arr = array();
if($result->num_rows >= 0) {
while($row = $result->fetch_assoc()) {
array_push($ret_arr,$row);
}
} else {
array_push($ret_arr,null);
echo '{"msg":"error","flag":false}';
die("error");
}
echo json_encode($ret_arr);
$conn->close();
?>
使⽤以上代码时需要:
1:在后台建⽴⾃⼰的数据库并且创建数据表,数据表包含前端页⾯所对应的字段名称2:把域名,端⼝号,数据库密码,数据库名称,数据表名改为⾃⼰的
3:结合前端发送ajax 到后台发送数据和获取数据来使⽤
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论