PHP连接MYSQL数据库参数设置机器增删查改操作
1.param.php
<?php session_start(); if(!$_SESSION["user"]->poststuno) exit('你的工号不正确'); if($_SESSION["user"]->juese=='001') exit('未知错误,请联系管理员 0001'); $db_host_main="192.168.*.*";//主数据库地址 $db_user="root";//数据库用户名 $db_name="**";//数据库名 $db_passw="***";//数据库密码 $czr=trim($_SESSION["user"]->poststuno); $czrxm=trim($_SESSION["user"]->xm); $cztime=date("Y-m-d H:i:s"); $mem_server="192.168.95.103"; $czip = $_SERVER["HTTP_X_FORWARDED_FOR"]; //操作人ip地址
2.添加操作
<?php //将如下的代码放入PHP的文件中 error_reporting(E_ALL);//显示所有的错误信息 ini_set("display_errors", "Off"); //设置文件上传大小 session_start(); require('param.php'); //连接数据库 $pdo = new PDO("mysql:host=".$db_host_main.";dbname=".$db_name.";charset=utf8;",$db_user,$db_passw); $bmmc = isset($_POST['bmmc']) ? $_POST['bmmc'] : '部门'; $juese = isset($_POST['juese']) ? $_POST['juese'] : '学工'; $fzr = isset($_POST['fzr']) ? $_POST['fzr'] : '20192100063'; $fzr_xm = isset($_POST['fzr_xm']) ? $_POST['fzr_xm'] : '熊天明'; $czr = isset($_POST['czr']) ? $_POST['czr'] : '李四'; $zt = '1'; $sql = "SELECT * FROM 表 where fzr =? AND juese=? AND bmmc=? AND zt=?"; $result = $pdo->prepare($sql); $result->execute(array($fzr,$juese,$bmmc,$zt)) ; $e = $result->fetchAll(); if ($e){ echo '数据已存在!14'; }else{ $insert = "INSERT INTO 表 SET bmmc = ? , fzr = ? , fzr_xm = ? , juese = ?, czr = ?"; $result = $pdo->prepare($insert); $result->execute(array($bmmc,$fzr,$fzr_xm,$juese,$czr)) or die ('查询错误!14'); echo '插入成功!'; if($result > 0 ){ echo json_encode(array( "code" => 1, "msg" => "success", "count" => $result, "data" => array() )); }else{ echo json_encode(array( "code" => 1, "msg" => "插入失败!", "count" => 0, "data" => array() )); } }
3.查询操作
<?php //将如下的代码放入PHP的文件中 error_reporting(E_ALL);//显示所有的错误信息 ini_set("display_errors", "Off"); //设置文件上传大小 session_start(); require('param.php'); //连接数据库 $conn = new PDO("mysql:host=".$db_host_main.";dbname=".$db_name.";charset=utf8;",$db_user,$db_passw); $page = isset($_GET['page']) ? $_GET['page']:'5'; //分页 isset($_POST['id']) ? $_POST['id'] : '这个是数据库id'; $limit = isset($_GET['limit']) ? $_GET['limit']:'50'; //每页显示条数 $start = ($_GET['page'] - 1) * $_GET['limit'] ; //起始页面 $fzr_xm = isset($_GET['fzr_xm']) ? $_GET['fzr_xm'] : "熊天明"; //where 条件,这里是姓名做测试,模糊查询 $sql = "SELECT * FROM bangfu_bm_qx where zt=1 order by id desc limit $start, $limit"; $count = "SELECT id FROM bangfu_bm_qx where zt=1"; //获取记录总数 $num = count($conn->query($count)->fetchAll(PDO::FETCH_ASSOC)); // 预处理语句 $stmt = $conn->prepare($sql); // 执行查询 $stmt->execute(); // 获取所有结果 $results = $stmt->fetchAll(PDO::FETCH_ASSOC); //echo '<br>'; $json = json_encode(array( "code" => 0, "msg" => "", "count" => $num, "data" => $results ), JSON_UNESCAPED_UNICODE); //JSON_UNESCAPED_UNICODE注意不加会乱码 echo $json;
4.删除操作
<?php //将如下的代码放入PHP的文件中 error_reporting(E_ALL);//显示所有的错误信息 ini_set("display_errors", "Off"); //设置文件上传大小 session_start(); require('param.php'); $id = isset($_POST['id']) ? $_POST['id'] : '这个是数据库id'; $pdo = new PDO("mysql:host=".$db_host_main.";dbname=".$db_name.";charset=utf8;",$db_user,$db_passw); $sql = "UPDATE bangfu_bm_qx set zt='-1' WHERE id=?"; $stmt = $pdo->prepare($sql); $stmt->bindParam(1, $id); $stmt->execute(); $pdo = null;