MySQL Tutorial i data connection class
Class Mydb extends Mysqli
{
protected $_databasehost = ' localhost ';
protected $_databaseuser = ' root ';
protected $_databasepass = ' 123456 ';
Protected $_databasename = ' test ';
protected $_databaseport = ' 3306 ';
protected $_fetch_method = MYSQLI_ASSOC;
protected $_databasechar = ' UTF8 ';
/**
* Enter description here ...
*
* @param string $host
* @param string $user
* @param string $pass
* @param string $name
* @param int $port
* @param string $char
*/
function __construct ($host = ', $user = ', $pass = ', $name = ', $port = ', $char = ')
{
$this->_databasehost = Empty ($host)? $this->_databasehost: $host;
$this->_databaseuser = Empty ($user)? $this->_databaseuser: $user;
$this->_databasepass = Empty ($pass)? $this->_databasepass: $pass;
$this->_databasename = Empty ($name)? $this->_databasename: $name;
$this->_databaseport = Empty ($port)? $this->_databaseport: $port;
$this->_databasechar = Empty ($char)? $this->_databasechar: $char;
try{
Parent::__construct ($this->_databasehost, $this->_databaseuser,
$this->_databasepass, $this->_databasename,
$this->_databaseport);
if (Mysqli_connect_errno ()) {
Die ("Server Connection Failed");
}
if (!parent::set_charset ($this->_databasechar)) {
Die ("Database tutorial cannot use UTF-8 encoding");
}
}catch (Exception $e) {
Die ($e->getmessage ());
}
}
function Selectdb ($name)
{
$this->_databasename = Empty ($name)? $this->_databasename: $name;
return $this->select_db ($this->_databasename);
}
/**
* Enter description here ...
*
*/
function __desstruct ()
{
$this->close ();
}
/**
* Setting return value type
*
* @param int $type
*/
function Setfetchmeth OD ($type =2)
{
switch ($type)
{
case ' 1 ':
$this->_fetch_method = mysqli_num;
break;
case ' 2 ':
$this->_fetch_method = MYSQLI_ASSOC;
break;
case ' 3 ':
$this->_fetch_method = mysqli_both;
break;
default:
$this->_fetch_method = MYSQLI_ASSOC;
break;
}
}
/**
* Insert before escape
*
* @param string|array $str
* @return string
*/
function Quoteinto ($str)
{
if (Is_array ($str)) {
foreach ($str as $key => $val) {
$str [$key] = $this->real_escape_string ($val);
}
}else{
$str = $this->real_escape_string ($str);
return $str;
}
/**
* Insert
*
* @param string $table
* @param array $fields
* @return int
* @deprecated The key value for the inserted field value is the inserted value
*/
function Insert ($table, $fields)
{
if (empty ($table)) {return false;}
if (Empty ($fields)) {return false;}
$insertFields = @implode (', ', @array_keys ($fields));
$arrayValues = @array_values ($fields);
$result = $this->quoteinto ($arrayValues);
$insertValues = "'". @implode ("', '", $result). "
$sql = INSERT into. $table. " (". $insertFields.")
values (". $insertValues.");
$this->query ($sql);
return $this->insert_id;
}
/**
* Update
*
* @param string $table
* @param array $fields
* @param string $where
* @return NULL
* @deprecated key is the inserted value of the field value key value
*/
functio N Update ($table, $fields, $where)
{
if (Empty ($table)) {return false;}
if (Empty ($fields)) {return false;}
if (Empty ($where)) {return false;}
foreach ($fields as $key => $v)
{
$condition [] = {$key}= ' ". $ This->quoteinto ($v). "'";
}
$sql = "UPDATE". $table. "
set. Implode (', ', $condition). " WHERE ". $where;
$this->query ($sql);
}
/**
* Get a piece of data
*
* @param string $sql
* @return Array one-dimensional arrays
*/
function Fetchrow ($sql)
{
$result = $this->query ($sql);
$row = $result->fetch_array ($this->_fetch_method);
return $row;
}
/**
* Get all the data
*
* @param string $sql
* @return Array of two dimensions
*/
function Fetchall ($sql)
{
$result = $this->query ($sql);
while ($row = $result->fetch_array ($this->_fetch_method))
{
$data [] = $row;
}
return $data;
}
function Fetchfree ()
{
return $this->free_result ();
}
/**
* Enter description here ...
*
* @param string $type
* @return Unknown
*/
function getversion ($t Ype= ' server '
{
switch ($type) {
case ' server ':
$result = $this->server_version;
break;
case ' client ':
$result = $this->client_version;
break;
default:
$result = $this->server_version;
break;
}
return $result;
}
/**
* Enter description here ...
*
* @param string $type
* @return Unknown
*/
function GetInfo ($type = ' server '
{
switch ($type) {
case ' server ':
$result = $this->server_info;
break;
case ' client ':
$result = $this->client_info;
break;
default:
$result = $this->server_info;
break;
}
return $result;
}
}