一. connect.php 文件,连接数据库
<?php
header('content-type:text/html;charset=utf-8');
$mysql_conf = array(
'host'=>'localhost:3306',
'db_user'=>'root',
'db_pass'=>'2020',
'db'=>'userinfo'
);
$mysqli = @new mysqli($mysql_conf['host'],$mysql_conf['db_user'],$mysql_conf['db_pass']);
if($mysqli->connect_errno){
die('连接错误'.$mysqli->connect_errno);
}
$mysqli->query("set names utf8");
$select_db = $mysqli->select_db($mysql_conf['db']);
if(!$select_db){
die('数据库选择错误'.$mysqli->error);
}
?>
二. php语法操作数据库
<?php
header('Access-Control-Allow-Origin:*');
include('./connect.php');
$name = $_REQUEST['name'];
$telphone=$_POST["telphone"];
$sqlAll="select * from users";
$sql = "select * from users where telphone='$telphone'";
if($telphone=='13668686868'){
$result = $mysqli->query($sqlAll);
}else{
$result = $mysqli->query($sql);
}
$results = array();
while ($row = mysql_fetch_assoc($result)) {
$results[] = $row;
}
echo json_encode($results);
$sql = "select * from users where telphone='$telphone'";
$result = $mysqli->query($sql);
if($result->num_rows>0){
echo '{"msg":"用户名已存在"}';
$mysqli->close();
die;
}
$insertSql = "insert into users(user_name,user_telphone) values('$name','$telphone')";
$res = $mysqli->query($insertSql);
$sql = "UPDATE mingziuser SET pay='$pay' WHERE id='$id'";
$result = $mysqli->query($sql);
$mysqli->close();
?>