userName:
userPwd:
class itcastUsers {
private $db;
// 构造函数-建立数据库链接
function __construct() {
$this->db = new mysqli('127.0.0.1', 'root', '111093', 'itcast');
if (mysqli_connect_errno()){
printf("连接错误:%s\n", mysqli_connect_error());
exit();
}
$this->db->autocommit(FALSE);
}
// 析构函数-关闭数据库连接
function __destruct() {
$this->db->close();
}
// 用户登录
function userLogin() {
if (isset($_GET['username']) && isset($_GET['password'])){
// 获取GET请求参数
$accessType = '[GET]';
$userName = $_GET['username'];
$userPassword = $_GET['password'];
} else if (isset($_POST['username']) && isset($_POST['password'])){
// 获取POST请求参数
$accessType = '[POST]';
$userName = $_POST['username'];
$userPassword = $_POST['password'];
} else {
echo('非法请求!');
return false;
}
// 设置数据库查询字符编码
$this->db->query('set names utf8');
// 查询请求
$data = $this->db->query("SELECT id, userName, userImage, FROM userInfo WHERE userName='$userName' AND userPwd='$userPassword'");
// 绑定查询参数
$this->db->real_escape_string($userName);
$this->db->real_escape_string($userPassword);
// 提交查询请求
$this->db->commit();
// 提取一条查询结果
$row = $data->fetch_assoc();
// 将结果绑定到数据字典
$result = [
'userId' => $row['id'],
'userName' => $row['userName'],
'userImage' => $row['userImage']
];
// 将数据字典使用JSON编码
echo json_encode($result);
return true;
}
}
header('Content-Type:text/html;charset=utf-8');
$itcast = new itcastUsers;
$itcast->userLogin();
?>
我想吧html和php联系起来我想问一下我的html那里写的不对请帮我改正一下谢谢
原始头信息
Connection close
Content-Length 0
Content-Type text/html
Date Thu, 30 Apr 2015 08:39:40 GMT
Server Apache/2.2.24 (Unix) DAV/2 PHP/5.3.26 mod_ssl/2.2.24 OpenSSL/0.9.8y
X-Powered-By PHP/5.3.26
展开