Mysqli of Process Style

Source: Internet
Author: User
Tags getmessage

<?php
/**
*
* @author Administrator
* MYSQLI_CONNCET () obj
* Mysqli_set_charset () bool
* Mysqli_query () mixed input bool output result_obj
* Mysqli_fetch_array () row
* Mysqli_fetch_all () all
*/
Class dbmysqlimanage{
Public $link =null;
Public $message;
/**
* Operation Tip Information
* @param unknown $mes information
* @param string $flag success and failure flag
* @return Boolean
*/
Public Function GetMessage ($mes, $flag =false) {
if ($flag) {
$this->message.= ' <div style= "color:green;font-size:14px" > ". $mes. ' </div> ';
return true;
}else{
$this->message.= ' <div style= "color:red;font-size:14px" > ". $mes. ' </div> ';
return false;
}
}
/**
* Initialize link
* @param unknown $host host address
* @param unknown $username users
* @param unknown $password password
* @param unknown $dbName database name
* @param string $charset Character Set
*/
Public function __construct ($host, $username, $password, $dbName, $charset = ' utf8 ') {
$this->link=mysqli_connect ($host, $username, $password, $dbName);
if ($this->link===false) {
return $this->getmessage (' Database connection failure error number '. Mysqli_connect_errno ().
' Error message: '. Iconv (' GBK ', ' Utf-8 ', Mysqli_connect_error ()));
}else{
$this->getmessage (' Database connection succeeded ', true);
}
$char =mysqli_set_charset ($this->link, $charset);
if ($char ===false) return $this->getmessage (' Wrong encoding set error number: '.
Mysqli_errno ($this->link). ' Error message: '. Mysqli_error ($this->link));
$this->getmessage (' encoding set succeeded ', true);
}
/**
* Perform additions and deletions and change statements
* @param unknown $sql SQL statements
* @return Boolean
*/
Public Function Execsql ($sql) {
$sql =trim ($sql);
$match =preg_match ('/^ (insert|update|delete)/', $sql);
if ($match ===0) {
return $this->getmessage (__function__. ' () method only support additions and deletions to the operation ');
}else{
$result =mysqli_query ($this->link, $sql);
if ($result ===false) {
return $this->getmessage ($sql. ' Statement Error Error number: '.
Mysqli_errno ($this->link). ' Error message: '. Mysqli_error ($this->link));
}else{
return $this->getmessage (' Operation succeeded ', ' true ');
}
}
}
/**
* Query a record
* @param unknown $sql SQL statements
* @return Failure: boolean| success: one-dimensional array
*/
Public Function Getonedata ($sql) {
$sql =trim ($sql);
$match =preg_match ('/^select/', $sql);
if ($match ===0) {
return $this->getmessage (__function__. ' () method only supports query operation ');
}else{
$result =mysqli_query ($this->link, $sql); Mysqli_result Object
if (!is_object ($result)) {
return $this->getmessage ($sql. ' Statement Error Error number: '.
Mysqli_errno ($this->link). ' Error message: '. Mysqli_error ($this->link));
}else{
$row =mysqli_fetch_array ($result, MYSQLI_ASSOC);
if (Mysqli_fetch_array ($result, MYSQLI_ASSOC)) return $this->getmessage (__function__. ' () can only query one data error number: '.
Mysqli_errno ($this->link). ' Error message: '. Mysqli_error ($this->link));
$this->getmessage (' Query a record succeeds ', true);
Unset ($result);
return $row;
}
}
}
/**
* Query multiple records
* @param unknown $sql SQL statements
* @return Failure: boolean| success: two-bit array
*/
Public Function Getmoredata ($sql) {
$sql =trim ($sql);
$match =preg_match ('/^select/', $sql);
if ($match ===0) {
return $this->getmessage (__function__. ' () method only supports query operation ');
}else{
$result =mysqli_query ($this->link, $sql); Mysqli_result Object
if (!is_object ($result)) {
return $this->getmessage ($sql. ' Statement Error Error number: '.
Mysqli_errno ($this->link). ' Error message: '. Mysqli_error ($this->link));
}else{
$moreRow =mysqli_fetch_all ($result, MYSQLI_ASSOC);
Unset ($result);
return $moreRow;
}
}
}
/**
* Close Connection Resources
* @return Boolean
*/
Public Function Closedb () {
$close =mysqli_close ($this->link);
if ($close ===false) return $this->getmessage (' Database connection shutdown failed ');
return $this->getmessage (' database connection closed successfully ', true);
}
/**
* destructor
*
*/
Public Function __destruct () {
unset ($this->message);
}
}




Mysqli Procedural style

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.