zoukankan      html  css  js  c++  java
  • 数据库SQL常用增删改查语句

    1:连接数据库

    conn.php文件

    <?php

    // 连接到 数据库
    $servername = "域名和端口号";
    $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
    include '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';

    $id = $_REQUEST['id'];

    $sql = "delete from wq_list where wq_id = $id";
    $ret = $conn->query($sql);
    if($ret > 0) {
    echo '{"msg":"success","flag":true}';
    } else {
    echo '{"msg":"failed","flag":false}';
    }
    $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);
    $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 到后台发送数据和获取数据来使用
     
     
  • 相关阅读:
    瑞游天翼客户端win7,win8,win10
    js循环POST提交添加辅助单位
    服务器状态检测(1)
    快速切换天财商龙门店后台.VB6.0
    大批量删除农行点菜宝菜品品相和房间
    社会工程学
    你还会记得吗
    优酷爆个人数据漏洞
    win8删除无线网络其中的一项配置
    穿越火线修改成宽屏模式
  • 原文地址:https://www.cnblogs.com/senlin1314/p/10371749.html
Copyright © 2011-2022 走看看