Package database MySQL, mysqli

Source: Internet
Author: User
Tags mysql connect rtrim

<?php

Header ("Content-type:text/html;charset=utf-8");

Class db{
private static property
private static $dbcon = false;
Private method of construction
Private function __construct ($dbname) {
$dbcon = @mysql_connect (' 127.0.0.1 ', ' root ', ' root ');
mysql_select_db ($dbname, $dbcon) or Die ("MySQL connect error");
mysql_query ("Set names UTF8", $dbcon);
}
Private Cloning method
Private Function __clone () {}
public static methods, database connection objects
public static function getinstance ($dbname) {
if (self:: $dbcon = = False) {
Self:: $dbcon = new db ($dbname);
}
Return self:: $dbcon;
}
/**
* EXECUTE statement
* @param $sql
* @return Source
*/
Public Function Query ($sql) {
return mysql_query ($sql);
}
/**
* Query A field, such as SELECT COUNT (*), select username
* @param $sql
* @return string or int
*/
Public Function GetOne ($sql) {
$query = $this->query ($sql);
Return mysql_result ($query, 0);
}
/**
* Get a row of records
* @param $sql
* @return Array one-dimensional
*/
Public Function GetRow ($sql, $type = ' Assoc ') {
$query = $this->query ($sql);
if (!in_array ($type, Array ("Assoc", "Array", "Row"))) {
Die ("mysql_query error");
}
$functionname = "Mysql_fetch_". $type;
Return $functionname ($query);
}
/**
* Preconditions: Get a record from a resource
* @param $query Source
* @return Array one-dimensional
*/
Public Function Getrowfromsource ($query, $type = "Assoc") {
if (!in_array ($type, Array ("Assoc", "Array", "Row"))) {
Die ("mysql_query error");
}
$functionname = "Mysql_fetch_". $type;
Return $functionname ($query);
}
/**
* Get all records
* @param $sql
* @return Array Two-dimensional
*/
Public Function GetAll ($sql) {
$query = $this->query ($sql);
$list = Array ();
while ($row = $this->getrowfromsource ($query)) {
$list [] = $row;
}
return $list;
}
/*
* New Data method
* @param1 $table, $data table name data
* @return Last added operation to generate ID value
*/
Public Function Insert ($table, $data) {
Iterate through an array to get the values of each field and field
$kstr = ";
$vstr = ";
foreach ($data as $key = = $val) {
$key is the field name, $val is the corresponding value
$kstr = $key. ",";
$vstr = "' $val ',";
}
$kstr = RTrim ($kstr, ', ');
$vstr = RTrim ($vstr, ', ');
The SQL statement added
$sql = "INSERT into $table ($KSTR) VALUES ($VSTR)";
Perform
$this->query ($sql);
Returns the last increment of the operation to generate the ID value
return $this->getinsertid ();
}
/*
* Delete a data method
* @param1 $table, $where =array (' id ' = ' 1 ') Table name condition
* @return The number of rows affected
*/
Public Function Deleteone ($table, $where) {
if (Is_array ($where)) {
foreach ($where as $key = = $val) {
$condition = $key. ' = '. $val;
}
} else {
$condition = $where;
}
$sql = "Delete from $table where $condition";
$this->query ($sql);
Returns the number of rows affected
return Mysql_affected_rows ();
}
/*
* Delete multiple data methods
* @param1 $table, $where table name conditions
* @return The number of rows affected
*/
Public Function DeleteAll ($table, $where) {
if (Is_array ($where)) {
foreach ($where as $key = = $val) {
if (Is_array ($val)) {
$condition = $key. ' In ('. Implode (', ', $val). ') ';
} else {
$condition = $key. ' = '. $val;
}
}
} else {
$condition = $where;
}
$sql = "Delete from $table where $condition";
$this->query ($sql);
Returns the number of rows affected
return Mysql_affected_rows ();
}
/*
* Modify Data method
* @param1 $table, $data, $where table name data conditions
* @return The number of rows affected
*/
Public Function Update ($table, $data, $where) {
Iterate through an array to get the values of each field and field
$str = ";
foreach ($data as $key = = $v) {
$str. = "$key = ' $v ',";
}
$str =rtrim ($str, ', ');
Modified SQL statements
$sql = "Update $table set $str where $where";
$this->query ($sql);
Returns the number of rows affected
return Mysql_affected_rows ();
}
/**
* Get the ID of the data just inserted
* @return int
*/
Public Function Getinsertid () {
return mysql_insert_id ();
}
/**
* Print_r Print Data
*/
Public function P ($arr) {
echo "<pre>";
Print_r ($arr);
echo "</pre>";
}
/**
* Var_dump Print Data
*/
Public Function V ($arr) {
echo "<pre>";
Var_dump ($arr);
echo "</pre>";
}


}
$dbname = ' Yii2_project ';
$db = Db::getinstance ($dbname);
where article_id=1
$sql = "SELECT * from article";
$arr = $db->getall ($sql);
$db->p ($arr);


<?php

Header
Header (' Content-type:text/html;charset=utf-8 ');

Class DB {
Defining properties
private $host;//Host Name
private $port;//Port number
private $name;//user name
private $pass;//password
private $dbname;//Database name
private $charset;//Set character sets
private $link;//Connect to Database
private static $instance;
Initializing constructors
Private Function __construct ($arr = Array ()) {
$this->host = isset ($arr [' Host '])? $arr [' Host ']: ' localhost ';
$this->port = isset ($arr [' Port '])? $arr [' Port ']: ' 3306 ';
$this->name = isset ($arr [' name '])? $arr [' name ']: ' Root ';
$this->pass = isset ($arr [' Pass '])? $arr [' Pass ']: ' Root ';
$this->dbname = isset ($arr [' dbname '])? $arr [' dbname ']: ' Yii2_project ';
$this->charset = isset ($arr [' CharSet '])? $arr [' CharSet ']: ' UTF8 ';
Connecting to a database
$this->db_connect ();
Select Database
$this->db_usedb ();
Set character sets
$this->db_charset ();
}
Connecting to a database
Private Function Db_connect () {
Host Name: port number User name password
$this->link = Mysqli_connect ($this->host. ': ' $this->port, $this->name, $this->pass);
Connection failed
if (! $this->link) {
Echo ' Database connection failed <br> ';
Echo ' Error code is: '. Mysqli_errno ($this->link). ' <br> ';
The echo ' error message is: '. Mysqli_error ($this->link). ' <br> ';
Exit
}
}
Select Database
Private Function Db_usedb () {
Mysqli_query ($this->link, "Use {$this->dbname}");
}
Set character sets
Private Function Db_charset () {
Mysqli_query ($this->link, "set names {$this->charset}");
}
Privatization of cloning functions to prevent external cloning of objects
Private Function __clone ()
{
}

Single Access Unified portal
public static function getinstance ()
{
if (! ( Self:: $instance instanceof Self))
{
Self:: $instance = new DB ();
}
Return self:: $instance;
}
Methods for executing SQL statements
Private Function Db_query ($sql) {
$res = Mysqli_query ($this->link, $sql);
if (! $res) {
echo ' SQL statement has error <br> ';
Echo ' Error code is: '. Mysqli_errno ($this->link). ' <br> ';
The echo ' error message is: '. Mysqli_error ($this->link). ' <br> ';
Exit
}
return $res;//Return data successfully
}
Add action
Public Function Db_insert ($sql) {
$this->db_query ($sql);
Returns the last ID that was added
return mysql_insert_id ();
}
Delete operation
Public Function Db_delete ($sql) {
$this->db_query ($sql);
Returns the number of rows affected
Return Mysqli_affected_rows ($this->link);
}
Modify Operation
Public Function Db_update ($sql) {
$this->db_query ($sql);
Returns the number of rows affected
Return Mysqli_affected_rows ($this->link);
}
View a single piece of data
Public Function Db_getone ($sql) {
$res = $this->db_query ($sql);
Return data
Return Mysqli_fetch_assoc ($res);
}
View more than one piece of data
Public Function Db_getall ($sql) {
$res = $this->db_query ($sql);
$arr = Array ();
while ($row = Mysqli_fetch_assoc ($res)) {
$arr [] = $row;
}
Return data
return $arr;
}

}


Package database MySQL, mysqli

Related Article

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.