php
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/12/9 * Time: 13:37 */ $db = new mysqli('localhost','root','','test','3306'); !mysqli_connect_error() or die('数据库连接失败'); $db->query('set names utf8'); //接收前台数据 $type = $_REQUEST['type']; switch ($type){ case 'get_action': $username = $_REQUEST['username']; $sql = "SELECT aid FROM user WHERE username = '$username'"; $res = $db->query($sql); $aid = $res->fetch_row(); $sqls = "SELECT * FROM action WHERE aid in ($aid[0])"; // die($sqls); $arr = $db ->query($sqls); $brr = array(); while($crr = $arr->fetch_assoc()){ array_push($brr,$crr); } echo json_encode($brr); break; case 'all_action': $username = $_REQUEST['username']; $sqls = "SELECT aid FROM user WHERE username = '$username'"; $res = $db->query($sqls); $aid = $res->fetch_row(); $aidArr = explode(",",$aid[0]); // die($aid[0]); $sql = "SELECT aid FROM action"; $resu = $db->query($sql); $aidAll = $resu->fetch_all(); var_dump($arr); die; // var_dump($aidArr,$aidAll); // 返回数组不同值 $result = array_diff($aidArr,$aidAll[0]); var_dump($result); die; $actionStr = implode(",",$result); var_dump($actionStr); die; $arr = array(); while ($brr = $res ->fetch_assoc()){ array_push($arr,$brr); } echo json_encode($arr); break; case 'sub_action': $username = $_REQUEST['username']; $aidStr = $_REQUEST['aidStr']; $aid = ','.$aidStr; $sql = "UPDATE user SET aid= CONCAT(aid,'$aid') WHERE username = '$username'"; $res = $db->query($sql); break; }