zoukankan      html  css  js  c++  java
  • php demo

    <?php
      header("Access-Control-Allow-Origin: *");
        //测试php是否可以拿到数据库中的数据
        /*echo "44444";*/
        //做个路由 action为url中的参数
        $action = $_GET['action'];
         /**查询服务器中的数据
         * 1、连接数据库,参数分别为 服务器地址 / 用户名 / 密码 / 数据库名称
         * 2、返回一个包含参数列表的数组
         * 3、遍历$sqls这个数组,并把返回的值赋值给 $s
         * 4、执行一条mysql的查询语句
         * 5、关闭数据库
         * 6、返回执行后的数据
         */
        function query_sql(){
            $mysqli = new mysqli("localhost", "table", "admin123", "table");
            //设置编码
            mysqli_set_charset($mysqli, "utf8");
            $sqls = func_get_args();
            foreach($sqls as $s){
                $query = $mysqli->query($s);
            }
            $mysqli->close();
            return $query;
        }
        switch($action) {
            case 'search':
                search();
                break;
            case 'init_data_list':
                init_data_list();
                break;
            case 'detail':
                detail();
                break;
            case 'add_row':
                add_row();
                break;
            case 'del_row':
                del_row();
                break;
            case 'edit_row':
                edit_row();
                break;
        }
        function search(){
            $name=$_GET['name']; 
            if(!$name){
                echo "请输入搜索关键词1111";
            }
            $sql = "SELECT * FROM  ce_work_order WHERE name LIKE '%{$name}%' ||content LIKE '%{$name}%' ||admin_name LIKE '%{$name}%'"; 
            $query = query_sql($sql); 
            while($row = $query->fetch_assoc()){
                $row[take_time]= date( "Y-m-d ",$row[take_time]);
                $data[] = $row;
            }
            $json = json_encode(array(
                "resultCode"=>200,
                "message"=>"查询成功!",                
                "data"=>$data
            ),JSON_UNESCAPED_UNICODE);
            //转换成字符串JSON
            echo($json);   
        }
        //详情
        function detail(){
                $id=isset($_GET['id'])? $_GET['id']:1;                
                $sql = "select * from ce_work_order  where id=$id";
                $query = query_sql($sql);
                while($row = $query->fetch_assoc()){
                    $row[take_time]= date( "Y-m-d ",$row[take_time]);
                    $row[finish_time]= date( "Y-m-d ",$row[finish_time]);
                    $row[create_time]= date( "Y-m-d ",$row[create_time]);
                    // echo $row
                    $data[] = $row;
                }
                $json = json_encode(array(
                    "resultCode"=>200,
                    "message"=>"查询成功!",                
                    "data"=>$data
                ),JSON_UNESCAPED_UNICODE);
                //转换成字符串JSON
                echo($json);                    
        }
        //查询方法
        function init_data_list(){
            //测试 运行crud.html时是否可以获取到 下面这个字符串
            /*echo "46545465465456465";*/
            $page=isset($_GET['page'])? $_GET['page']:1;
            $pagesize=isset($_GET['pagesize'])? $_GET['pagesize']:20;
                    //查询表        
            $sql = "select name,id,channel,admin_name,take_time,status,id from ce_work_order  order by take_time desc limit ".($page-1) * $pagesize .",$pagesize";
            $query = query_sql($sql);
            $query2 = query_sql("select count(id) as count  from ce_work_order");
            $row2 = $query2->fetch_assoc();
            while($row = $query->fetch_assoc()){
                $row[take_time]= date( "Y-m-d ",$row[take_time]);
                // echo $row
                $data[] = $row;
            }
            $json = json_encode(array(
                "resultCode"=>200,
                "message"=>"查询成功!",
                "total"=>$row2['count'],
                "data"=>$data
            ),JSON_UNESCAPED_UNICODE);
            //转换成字符串JSON
            echo($json);
        }
        //新增方法
        function add_row(){
            /*获取从客户端传过来的数据*/
            $userName = $_GET['user_name'];
            $userAge = $_GET['user_age'];
            $userSex = $_GET['user_sex'];
            //INSERT INTO 表名 (列名1,列名2,...)VALUES ('对应的数据1','对应的数据2',...)
            // VALUES 的值全为字符串,因为表属性设置为字符串
            $sql = "INSERT INTO t_users (user_name,user_age,user_sex) VALUES ('$userName','$userAge','$userSex')";
            if(query_sql($sql)){
                echo "ok!";
            }else{
                echo "新增成功!";
            }
        }
        //删除方法
        function del_row(){
            //测试
            /*echo "ok!";*/
            //接收传回的参数
            $rowId = $_GET['rowId'];
            $sql = "delete from t_users where user_id='$rowId'";
            if(query_sql($sql)){
                echo "ok!";
            }else{
                echo "删除失败!";
            }
        }
    ?>
  • 相关阅读:
    APMServ5.2.6 无法启动Apache的一个问题
    【转】流媒体技术笔记(视频编码相关)
    用APMServ一键快速搭建Apache+PHP+MySQL+Nginx+Memcached+ASP运行平台
    java swing 基础
    python class 类
    python 经验
    python 导入(转)
    kernel ipv4/ip_output.c
    python+正则表达式(转)
    Eclipse中如何快速添加、删除jar包
  • 原文地址:https://www.cnblogs.com/7c89/p/14286558.html
Copyright © 2011-2022 走看看