DBDA Database Classes

Source: Internet
Author: User
Tags dsn

<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.