1,类的封装
class ZYMySqlManager{
public $db;
//单例
static private $zymysqlmanger;
//判断实例初始化次数
private function __construct()
{
}
//单例初始化
/**
* @return
*/
static public function installmysql(){
//判断实例是否初始化
if (!isset(self::$zymysqlmanger)) {
self::$zymysqlmanger = new self();
$sqlManager = ZYMySqlManager::installmysql();
$sqlManager ->openMySql();
}
return self::$zymysqlmanger;
}
//防止被重写
private function __clone()
{
// TODO: Implement __clone() method.
}
public function openMySql(){
$this->db = mysqli_connect("localhost:3306","root","abc123654");
// mysqli_select_db("phpBRService",$this->db);
if ($this->db){//判断链接数据库是否成功
}else{
}
//创建db文件,如果存在不会重新创建
if (mysqli_query($this->db,"CREATE DATABASE phpBRServiceDB")){
}else{
}
//更改默认选中的db文件
mysqli_select_db($this->db,"phpBRServiceDB");
// 编码设置
mysqli_set_charset('utf8',$this->db);
}
public function creatTab($sqlStr){
//创建表
// $sqlc = "CREATE TABLE IF NOT EXISTS Persons
// (
// FirstName varchar(15),
// LastName varchar(15),
// Age int
// )";
mysqli_query($this->db,$sqlStr);
}
//插入数据
public function insterData($sqlStr){
//插入数据
// $sqlinser = "INSERT INTO Persons (FirstName, LastName, Age)
//VALUES ('Peter', 'Griffin', '35')";
mysqli_query($this->db,$sqlStr);
}
//查询数据
public function queryData($sqlStr,$keysArr){
// $result = mysqli_query($this->db,"SELECT * FROM Persons");
//
// while($row = mysqli_fetch_array($result))
// {
// echo $row['FirstName'] . " " . $row['LastName'];
// echo "</br>";
// }
//
// mysqli_close($this->db);
$searchResult = $this->db->query($sqlStr);
$arr = array();
//遍历查询结果
while ($item = mysqli_fetch_array($searchResult)){
$dataDic = array();
for ($i = 0; $i < count($keysArr); $i ++){
$key = $keysArr[$i];
//对null数据的处理
if ($item[$key]){
$dataDic[$key] = $item[$key];
}
}
$arr[] = $dataDic;
};
return $arr;
}
//删除数据
public function deleData($sqlStr){
mysqli_query($this->db,$sqlStr);
}
//更新数据
public function updateData($sqlStr){
$this->db->query($sqlStr);
}
}
2,注册实例 创建一个新的php file作为接口文件
<?php
require_once "../Objects/UserObject.php";
header('Content-Type:application/json;charset=utf-8');
$phone = $_GET["phone"];
$password = $_GET["password"];
$userObject = new UserObject();
$resultArr = $userObject->userRegister($phone,$password);
$result = json_encode($resultArr);
echo $result;
3,登录实例
<?php
require_once "../Objects/UserObject.php";
header('Content-Type:application/json;charset=utf-8');
$phone = $_GET["phone"];
$password = $_GET["password"];
$userObject = new UserObject();
$resultArr = $userObject->userLogin($phone,$password);
$result = json_encode($resultArr);
echo $result;
4,修改用户信息实例
<?php
require_once "../Objects/UserObject.php";
header('Content-Type:application/json;charset=utf-8');
$user_id = $_GET["id"];
$realname = $_GET["realname"];
$nickname = $_GET["nickname"];
$imgurl = $_GET["imgurl"];
$sign = $_GET["sign"];
$addr = $_GET["addr"];
$age = $_GET["age"];
$sex = $_GET["sex"];
$weichatid = $_GET["weichatid"];
$userObject = new UserObject();
$resultArr = $userObject->changeUserInfo($user_id,$realname,$nickname,$sign,$imgurl,$addr,$sex,$age,$weichatid);
$result = json_encode($resultArr);
echo $result;
5,查询用户信息实例
<?php
require_once "../Objects/UserObject.php";
header('Content-Type:application/json;charset=utf-8');
$user_id = $_GET["user_id"];
$userObject = new UserObject();
$resultArr = $userObject->getUserInfo($user_id);
$result = json_encode($resultArr);
echo $result;
6,封装的用户信息处理类
<?php
require_once "../ZYSql/ZYMySqlManager.php";
class UserObject{
/**
注册功能
*/
public function userRegister($phone,$password){
$mySql = ZYMySqlManager::installmysql();
$tabName = "UserList";
$resultArr = array();
if (!$phone || !$password || strlen($phone) != 11){
$resultArr["code"] = 201;
$resultArr["message"] = "phone or password is wrong";
return $resultArr;
}
//创建表
$sqlStr = "CREATE TABLE IF NOT EXISTS $tabName
(
nickname varchar(15),
realname varchar(15),
imgurl varchar(15),
phone varchar(15),
password varchar(15),
sign varchar(15),
addr varchar(15),
age int,
sex int,
id integer primary key auto_increment,
wechatid varchar(15),
creattime varchar(15)
)";
$mySql->creatTab($sqlStr);
//查询用户是否已经被注册
$keysArr = ["id","phone","realname","nickname","sign","sex","age","addr","imgurl","creattime"];
$querySql = "select * from $tabName where phone='$phone'";
$dataArr = $mySql->queryData($querySql,$keysArr);
if ($dataArr && count($dataArr) > 0){//表示用户已经被注册了
$resultArr["code"] = 202;
$resultArr["message"] = "phone is used";
return $resultArr;
}
//未注册则添加新的用户信息
$insertSql = "insert into $tabName (phone,password) values ('$phone','$password')";
$mySql->insterData($insertSql);
//再次查询用户信息,返回给客户端
$dataArr = $mySql->queryData($querySql,$keysArr);
if ($dataArr && count($dataArr) > 0){//表示用户已经被注册了
$resultArr["code"] = 200;
$resultArr["message"] = "success";
$resultArr["data"] = $dataArr;
return $resultArr;
}
$resultArr["code"] = 203;
$resultArr["message"] = "register fail";
return $resultArr;
}
/**
登录功能
*/
public function userLogin($phone,$password){
$mySql = ZYMySqlManager::installmysql();
$tabName = "UserList";
$resultArr = array();
if (!$phone || !$password || strlen($phone) != 11){
$resultArr["code"] = 201;
$resultArr["message"] = "phone or password is wrong";
return $resultArr;
}
//查询用户是否已经被注册
$keysArr = ["id","phone","realname","nickname","sign","sex","age","addr","imgurl","creattime"];
$querySql = "select * from $tabName where phone='$phone' and password='$password'";
$dataArr = $mySql->queryData($querySql,$keysArr);
if ($dataArr && count($dataArr) > 0){//表示用户已经被注册了
$resultArr["code"] = 200;
$resultArr["message"] = "success";
$resultArr["data"] = $dataArr;
return $resultArr;
}
$resultArr["code"] = 202;
$resultArr["message"] = "log fail no user";
return $resultArr;
}
/**
获取用户信息
*/
public function getUserInfo($user_id){
$mySql = ZYMySqlManager::installmysql();
$tabName = "UserList";
$resultArr = array();
if (!$user_id){
$resultArr["code"] = 201;
$resultArr["message"] = "缺少参数";
return $resultArr;
}
//查询用户是否已经被注册
$keysArr = ["id","phone","realname","nickname","sign","sex","age","addr","imgurl","creattime"];
$querySql = "select * from $tabName where id='$user_id'";
$dataArr = $mySql->queryData($querySql,$keysArr);
if ($dataArr && count($dataArr) > 0){//表示用户已经被注册了
$resultArr["code"] = 200;
$resultArr["message"] = "success";
$resultArr["data"] = $dataArr;
return $resultArr;
}
$resultArr["code"] = 202;
$resultArr["message"] = "没有此用户";
return $resultArr;
}
/**
修改用户信息
*/
public function changeUserInfo($user_id,$realname,$nickname,$sign,$imgurl,$addr,$sex,$age,$weichatid){
$mySql = ZYMySqlManager::installmysql();
$tabName = "UserList";
$resultArr = array();
if (!$user_id){
$resultArr["code"] = 201;
$resultArr["message"] = "缺少参数";
return $resultArr;
}
$updateStr = "update $tabName set ";
if ($realname){
$updateStr = $updateStr."realname='$realname' ";
}
if ($nickname){
$updateStr = $updateStr."nickname='$nickname' ";
}
if ($sign){
$updateStr = $updateStr."sign='$sign' ";
}
if ($imgurl){
$updateStr = $updateStr."imgurl='$imgurl' ";
}
if ($addr){
$updateStr = $updateStr."addr='$addr' ";
}
if ($sex){
$updateStr = $updateStr."sex=$sex ";
}
if ($age){
$updateStr = $updateStr."age=$age ";
}
if ($weichatid){
$updateStr = $updateStr."weichatid='$weichatid' ";
}
$updateStr = $updateStr."where id='$user_id'";
$mySql->updateData($updateStr);
echo $updateStr;
//查询用户
$keysArr = ["id","phone","realname","nickname","sign","sex","age","addr","imgurl","creattime"];
$querySql = "select * from $tabName where id='$user_id'";
$dataArr = $mySql->queryData($querySql,$keysArr);
if ($dataArr && count($dataArr) > 0){//表示用户已经被注册了
$resultArr["code"] = 200;
$resultArr["message"] = "success";
$resultArr["data"] = $dataArr;
return $resultArr;
}
$resultArr["code"] = 202;
$resultArr["message"] = "没有此用户";
return $resultArr;
}
}
?>