<?php
Class Dbda
{
Public $host = "localhost";//server address
Public $uid = "root";//username
Public $pwd = "";//Password
public $conn;//Connection object
Ways to manipulate databases
$SQL represents the SQL statement that needs to be executed
$type represents the type of SQL statement, 1 represents the query, 0 represents
$DB represents the name of the database to manipulate
Returns a two-dimensional array if it is a query
Returns TRUE or False if it is a different statement
function __construct ($db = "Z-stu") {
Connecting objects
$this->conn = new Mysqli ($this->host, $this->uid, $this->pwd, $db);
}
Public Function Query ($sql, $type =1) {
Judging if there is an error
!mysqli_connect_error () or Die ("Connection failed! ");
Execute SQL statement
$result = $this->conn->query ($sql);
Determine the type of SQL statement
if ($type ==1) {
If it is a query statement, returns a two-dimensional array of result sets
return $result->fetch_all ();
}else{
Returns TRUE or False if it is a different statement
return $result;
}
}
Ajax calls return JSON
Public Function Jsonquery ($sql, $type =1, $db = "MyDB") {
Defining a data source
$DSN = "mysql:dbname={$db};host={$this->host}";
Making PDO objects
$pdo = new PDO ($dsn, "{$this->uid}", "{$this->pwd}");
Prepare to execute SQL statement
$st = $pdo->prepare ($sql);
Executing a preprocessed SQL statement
if ($st->execute ()) {
if ($type ==1) {
$attr = $st->fetchall (PDO::FETCH_ASSOC);
Return Json_encode ($ATTR);
}else{
if ($st) {
return "OK";
}else{
return "NO";
}
}
}else{
echo "Failed to execute!" ";
}
}
Ajax Call Return string
Public Function strquery ($sql, $type =1) {
Determine if the connection is successful
!mysqli_connect_error () or Die ("Connection failed! ");
Execute SQL statement
$result = $this->conn->query ($sql);
Determine the type of SQL statement
if ($type ==1) {
$attr = $result->fetch_all ();
$str = "";
If the query statement returns a string
for ($i =0; $i <count ($attr); $i + +) {
for ($j =0; $j <count ($attr [$i]); $j + +) {
$str = $str. $attr [$i] [$j];
$str = $str. " ^";
}
$str = substr ($str, 0,strlen ($STR)-1);
$str = $str. "|";
}
$str = substr ($str, 0,strlen ($STR)-1);
return $str;
}else{
Returns TRUE or False if it is a different statement
if ($result) {
return "OK";
}else{
return "NO";
}
}
}
function Pdoquery ($sql, $type =1, $db = "MyDB") {
Creating Data sources
$dns = "mysql:host={$this->host};d bname={$db}";
Making PDO objects
$pdo = new PDO ($dns, $this->uid, $this->pwd);
Prepare an SQL statement
$stm = $pdo->prepare ($sql);
Executing a preprocessing statement
$r = $stm->execute ();
if ($r) {
if ($type ==1) {
return $stm->fetchall ();
}else{
return "OK";
}
}else{
return "NO";
}
}
}
DBDA Database Classes