Mini PHP Framework Include/mysql.class.php

Source: Internet
Author: User
Tags php framework

<?php

Defined (' ACC ') | | Exit (' Access Denied ');


Encapsulates the MySQL operation class, including the connection function, and the query function.


Class MySQL extends absdb{

protected static $ins = NULL;


protected $host; Host Name

protected $user; User name

protected $passwd; Password

protected $db; Database name

protected $port; Port

protected $conn = null;


In-house operation, get an object

public static function Getins () {

if (self:: $ins = = = null) {

Self:: $ins = new self ();

}


$conf = Conf::getins ();


Self:: $ins->host = $conf->host;

Self:: $ins->user = $conf->user;

Self:: $ins->passwd = $conf->pwd;

Self:: $ins->db = $conf->db;

Self:: $ins->port = $conf->port;


Self:: $ins->connect ();

Self:: $ins->select_db ();

Self:: $ins->setchar ();


Return self:: $ins;

}


Do not let external do new operations,

protected function __construct () {

}



Connecting to a database

Public Function connect () {

$this->conn = @mysql_connect ($this->host, $this->user, $this->passwd, $this->port);

if (! $this->conn) {

$error = new Exception (' Database Not connected ', 9);

Throw $error;

}

}


Send SQL query

Public Function Query ($sql) {

$rs = mysql_query ($sql, $this->conn);

if (! $rs) {

Log::write ($sql);

}

return $rs;

}


Encapsulation of a GetAll method

Parameters: $sql

return: Array,false

Public Function GetAll ($sql) {

$rs = $this->query ($sql);

if (! $rs) {

return false;

}


$list = Array ();

while ($row = Mysql_fetch_assoc ($rs)) {

$list [] = $row;

}

return $list;

}


Encapsulation of a GetRow method

Parameters: $sql

return: Array,false

Public Function GetRow ($sql) {

$rs = $this->query ($sql);

if (! $rs) {

return false;

}


Return Mysql_fetch_assoc ($RS);

}


Encapsulates a GetOne method,

Parameters: $sql

Return: Int,str (single value)

Public Function GetOne ($sql) {

$rs = $this->query ($sql);

if (! $rs) {

return false;

}


$tmp = Mysql_fetch_row ($RS);

return $tmp [0];

}


Select Library function

Public Function select_db () {

$sql = ' use '. $this->db;

return $this->query ($sql);

}


Functions for setting character sets

Public Function SetChar () {

$sql = ' Set names UTF8 ';

return $this->query ($sql);

}


Automatically generate INSERT statements, UPDATE statements, and execute

Public Function Autoexecute ($data, $table, $act = ' Insert ', $where = ') {

if ($act = = ' Insert ') {

$sql = ' INSERT INTO '. $table. ‘ (‘;

$sql. = Implode (', ', (Array_keys ($data)));

$sql. = ') values (\ ';

$sql. = Implode ("', '", Array_values ($data));

$sql. = "')";

} else if ($act = = ' Update ') {

if (!trim ($where)) {

return false;

}

$sql = ' Update '. $table. ' Set ';

foreach ($data as $k = = $v) {

$sql. = $k;

$sql. = ' = ';

$sql. = "'". $v. "',";

}

$sql = substr ($sql, 0,-1);

$sql. = ' where ';

$sql. = $where;

} else {

return false;

}


return $sql;

return $this->query ($sql);


}

}


/*

$db = mysql::getins (' localhost ', ' root ', ' 111111 ', ' php0620 ');


Print_r ($DB);


*/




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.