安装 phpStudyPro
Config.php:
<?php
/**
* Database config variables
*/
//本地数据库配置
define("DB_HOST", "localhost");
define("DB_PORT", 3307);
define("DB_USER", "root");
define("DB_PASSWORD", "root");
define("DB_DATABASE", "phpstudy");:
-------------------- DBFunctions.php -------------------------------
<?php
require_once 'Config.php';
class DB_Functions
{
private $conn;
private $isNext;
// constructor
function __construct()
{
$this->connect();
}
// destructor
function __destruct()
{
}
// Connecting to database
public function connect() {
// 创建连接
$this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD,DB_DATABASE,DB_PORT);
// 检测连接
if ($this->conn->connect_error) {
$this->isNext=false;
}else{
//检查是否创建数据库
// 创建数据库
$sql = "show DATABASES LIKE '".DB_DATABASE."'";
$row = $this->conn->query($sql)->fetch_object();
if ($row) {
$this->isNext=true;
} else {
// 创建数据库
$sql = "CREATE DATABASE ".DB_DATABASE;
if ($this->conn->query($sql) === TRUE) {
$this->isNext=true;
} else {
$this->isNext=false;
}
}
}
// return database handler
return $this->conn;
}
public function checkTablesUsers()
{
$sql = "SHOW TABLES LIKE 'users'";
$row = $this->conn->query($sql)->fetch_object();
if (!$row) {
// 使用 sql 创建数据表
$sql = "CREATE TABLE users (
uid INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
password VARCHAR(50),
avatar VARCHAR(50),
sign VARCHAR(50),
createtime DATETIME
)";
if ($this->conn->query($sql) === TRUE) {
return true;
} else {
$response["code"] = "0";
$response["msg"] = "创建数据表错误: " .$this->conn->error;
die(json_encode($response,JSON_UNESCAPED_UNICODE));
return false;
}
} else {
return true;
}
}
/**
* Storing new user
* returns user details
*/
public function storeUser($username, $password)
{
if ($this->checkTablesUsers()) {
$stmt = $this->conn->prepare("INSERT INTO users(username, password,createtime) VALUES(?, ?, NOW())");
//该函数绑定了 SQL 的参数,且告诉数据库参数的值。 "sss" 参数列处理其余参数的数据类型。s 字符告诉数据库该参数为字符串
$stmt->bind_param("ss", $username, $password);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
}
/**
* Get user by username and password
*/
public function getUserByUsernameAndPassword($username, $password)
{
if ($this->checkTablesUsers()) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
// verifying user password
$passwordss = $user['password'];
// check for password equality
if ($passwordss == $password) {
// user authentication details are correct
return $user;
}
} else {
return NULL;
}
}
}
/**
* Get user by uid
*/
public function getUserByUid($uid)
{
if ($this->checkTablesUsers()) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE uid = ?");
$stmt->bind_param("s", $uid);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return NULL;
}
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($username)
{
if ($this->checkTablesUsers()) {
$stmt = $this->conn->prepare("SELECT username from users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
return false;
}
}
}
}
3. index.php
<?php
require_once 'DBFunctions.php';
$db = new DB_Functions();
$user = $db->getUserByUsernameAndPassword("yuanwu", "123");
echo $user
?>
网友评论