PHP自作のMYSQL操作クラス
ここに記録を作る
PHP類のいくつかの知識点
self:$静的属性名
$this->一般プロパティ名またはメソッド名
単例類の4つのポイント(三私一公)
1構築オブジェクトの禁止---new()private__の禁止construct(){}プライベート空定義
2クローンオブジェクトを禁止--clone private_clone(){}プライベート空定義
3単一オブジェクト属性private static$オブジェクト名のプライベート静的空の定義を定義する
4エントリメソッドpublic static functionメソッド名()共通静的を定義する
対向
PHP類のいくつかの知識点
self:$静的属性名
$this->一般プロパティ名またはメソッド名
単例類の4つのポイント(三私一公)
1構築オブジェクトの禁止---new()private__の禁止construct(){}プライベート空定義
2クローンオブジェクトを禁止--clone private_clone(){}プライベート空定義
3単一オブジェクト属性private static$オブジェクト名のプライベート静的空の定義を定義する
4エントリメソッドpublic static functionメソッド名()共通静的を定義する
対向
'127.0.0.1',
'port' =>'3306',
'username' =>'root',
'password' =>'',
'dbname' =>'mysql',
'charset' =>'utf8',
);
//*********************************
// ( )
//
private static $db;
// : ---new()
private function __construct(){}
// :
private function __clone(){}
//
public static function getdb($arr=null){
// ,
if(!isset(self::$db)){self::$db=new self();}
//
if($arr){
if(isset($arr['host']))self::$db->cs['host']=$arr['host'];
if(isset($arr['port']))self::$db->cs['port']=$arr['port'];
if(isset($arr['username']))self::$db->cs['username']=$arr['username'];
if(isset($arr['password']))self::$db->cs['password']=$arr['password'];
if(isset($arr['dbname']))self::$db->cs['dbname']=$arr['dbname'];
if(isset($arr['charset']))self::$db->cs['charset']=$arr['charset'];
}
return self::$db;
}
//*****************************************
//
private function connect(){
//$this->db = @ new mysqli(
$this->mysqli = @ new mysqli(
$this->cs["host"].":".$this->cs["port"],
$this->cs["username"],
$this->cs["password"]
);
/* */
if (mysqli_connect_errno()) {
echo "
*************************************";
echo "
!!!";
echo "
:".mysqli_connect_errno();
echo "
:".mysqli_connect_error();
echo "
*************************************";
exit("
stop!!!");
}
}
//
private function charset($char="utf8"){
$this->mysqli->query("set names $char");
}
//
private function usedb($dbname="mysql"){
$this->mysqli->query("use $dbname");
}
//
private function ready(){
$this->charset($this->cs['charset']);
$this->usedb($this->cs['dbname']);
}
// SQL
//$sql--- :string
//$erro---- : NULL 0123....
private function query($sql,$erro=null){
$this->connect();
$this->ready();
$this->result = $this->mysqli->query($sql);
if($erro!==null){
if(!$this->result){
echo "
****************************";
echo "
";
echo "
:".$sql;
echo "
:".$this->mysqli->errno;
echo "
:".$this->mysqli->error;
echo "
****************************";
die("
STOP!!!");
}
}
return $this->result;
}
//
// $this-arr
// array true/false
// :
//$sql---MYSQL
//$index--- ,
// : 'n'---
// 'a'---
// 'b'--- / ( )
public function queryarr($sql=null,$index='a'){
if($sql===null){return;}
//$r=$this->query($sql,0);//
$res=$this->query($sql);
//
//1. , .--- :true
//2. --- :false
//3. , , ---- :object
//
// , .array{}
if($res===true){
$this->arr=true;
}else{
if($res===false){
$this->arr=false;
}else{
//
switch ($index) {
case 'a':
$this->arr=$res->fetch_all(MYSQLI_ASSOC);//
break;
case 'n':
$this->arr=$res->fetch_all(MYSQLI_NUM);//
break;
case 'b':
$this->arr=$res->fetch_all(MYSQLI_BOTH);//
break;
}
$res->free();//
}
}
$this->mysqli->close();//
$this->mysqli=null;//
$this->result=null;//
return $this->arr;
}
//**********************
// table
public function tab($arr)
{
if(gettype($arr)!='array'){
if($arr===true){
$arr=array(array('true'));
}else{
$arr=array(array('false'));
}
}
if($arr==null)$arr=array(array("null"));
echo "";
foreach($arr as $k1=>$v1)
{
//**********************
//
if($k1==0)
{
echo "";
foreach($v1 as $k2=>$v2)
{
echo "";
}
echo "";
}
//**********************
//
echo "";
foreach($v1 as $k2=>$v2)
{
echo "";
}
echo "";
}
echo "";
echo $k2;
echo " ";
echo $v2;
echo "
";
}
//SQL
public function escape($str){
$this->connect();
$s=$this->mysqli->escape_string($str);
$this->mysqli->close();//
$this->mysqli=null;
return $s;
}
//
public function test(){
/*
$a=123;
$b=$this->escape($a);
echo "
asdg asdf $b";
*/
//$sql="select session_content from session where session_id='$sess_id'";
//
//$sql="replace into session values('abcd','cccc',unix_timestamp())";
//$sql="replace into session values('aaaa','cccc',unix_timestamp())";
//$sql="insert into session values('abcd11','cccc1',unix_timestamp())";
//$sql="select * from session where session_id='abcd'";
//$sql="select * from session where session_id='abcd222'";
//$sql="set a=1";
//$sql="select * from session ";
//$sql="select * from session where session_id='777'";
//$this->queryarr($sql);
//$this->connect();
//$this->ready();
//echo "
".$this->cs['charset'];
//echo "";
//var_dump($this->arr);
//echo "
";
//$this->tab($this->arr);
}
}
$cs=array(
'host' =>'127.0.0.1',
'port' =>'3306',
'username' =>'xxxx',
'password' =>'1234abcd',
'dbname' =>'xxxx',
'charset' =>'utf8',
);
$op1=mysqli_object_db::getdb($cs);
echo " ";
//var_dump($op1);
echo "
";
$op1->test();
プロセス け'127.0.0.1',
'port' =>'3306',
'username' =>'root',
'password' =>'',
'dbname' =>'mysql',
'charset' =>'utf8',
);
//*********************************
// ( )
//
private static $db;
// : ---new()
private function __construct(){}
// :
private function __clone(){}
//
public static function getdb($arr=null){
// ,
if(!isset(self::$db)){self::$db=new self();}
//
if($arr){
if(isset($arr['host']))self::$db->cs['host']=$arr['host'];
if(isset($arr['port']))self::$db->cs['port']=$arr['port'];
if(isset($arr['username']))self::$db->cs['username']=$arr['username'];
if(isset($arr['password']))self::$db->cs['password']=$arr['password'];
if(isset($arr['dbname']))self::$db->cs['dbname']=$arr['dbname'];
if(isset($arr['charset']))self::$db->cs['charset']=$arr['charset'];
}
return self::$db;
}
//*****************************************
//
private function connect(){
//$this->db = @ new mysql(
$this->link = @mysqli_connect(
$this->cs["host"].":".$this->cs["port"],
$this->cs["username"],
$this->cs["password"]
);
/* */
if (mysqli_connect_errno()) {
echo "
*************************************";
echo "
!!!";
echo "
:".mysqli_connect_errno();
echo "
:".mysqli_connect_error();
echo "
*************************************";
exit("
stop!!!");
}
}
//
private function charset($char="utf8"){
mysqli_query($this->link,"set names $char");
}
//
private function usedb($dbname="mysql"){
mysqli_query($this->link,"use $dbname");
}
//
private function ready(){
$this->charset($this->cs['charset']);
$this->usedb($this->cs['dbname']);
}
// SQL
//$sql--- :string
//$erro---- : NULL 0123....
private function query($sql,$erro=null){
$this->connect();
$this->ready();
$this->result =mysqli_query($this->link,$sql);
if($erro!==null){
if(!$this->result){
echo "
****************************";
echo "
";
echo "
:".$sql;
echo "
:".mysqli_errno($this->link);
echo "
:".mysqli_error($this->link);
echo "
****************************";
die("
STOP!!!");
}
}
return $this->result;
}
//
// $this-arr
// array true/false
// :
//$sql---MYSQL
//$index--- ,
// : 'n'---
// 'a'---
// 'b'--- / ( )
public function queryarr($sql=null,$index='a'){
if($sql===null){return;}
//$res=$this->query($sql,0);//
$res=$this->query($sql);
//
//1. , .--- :true
//2. --- :false
//3. , , ---- :object
//
if($res===true){
$this->arr=true;
}else{
if($res===false){
$this->arr=false;
}else{
//
//MYSQLI_ASSOC/ , MYSQLI_NUM/ , MYSQLI_BOTH/
switch ($index) {
case 'a'://
$this->arr=mysqli_fetch_all($res,MYSQLI_ASSOC);
break;
case 'n'://
$this->arr=mysqli_fetch_all($res,MYSQLI_NUM);
break;
case 'b':
$this->arr=mysqli_fetch_all($res,MYSQLI_BOTH);
break;
}
mysqli_free_result ($res);//
}
}
mysqli_close ($this->link);//
$this->link=null;//
$this->result=null;//
return $this->arr;
}
//**********************
// table
public function tab($arr)
{
if(gettype($arr)!='array'){
if($arr===true){
$arr=array(array('true'));
}else{
$arr=array(array('false'));
}
}
if($arr==null)$arr=array(array("null"));
echo "";
foreach($arr as $k1=>$v1)
{
//**********************
//
if($k1==0)
{
echo "";
foreach($v1 as $k2=>$v2)
{
echo "";
}
echo "";
}
//**********************
//
echo "";
foreach($v1 as $k2=>$v2)
{
echo "";
}
echo "";
}
echo "";
echo $k2;
echo " ";
echo $v2;
echo "
";
}
public function escape($str){
$this->connect();
$s=mysqli_escape_string($this->link,$str);
mysqli_close ($this->link);//
$this->link=null;
return $s;
}
//
public function test(){
/*
$a=123;
$this->connect();
$b=mysqli_escape_string($this->link,$a);
mysqli_close ($this->link);//
$this->link=null;
$c="asdf asd $b";
echo $c;
*/
//$sql="select session_content from session where session_id='$sess_id'";
//
//$sql="replace into session values('abcd','cccc',unix_timestamp())";
//$sql="replace into session values('aaaa','cccc',unix_timestamp())";
//$sql="insert into session values('abcd11','cccc1',unix_timestamp())";
//$sql="select * from session where session_id='abcd'";
//$sql="select * from session where session_id='abcd222'";
//$sql="set a=1";
$sql="select * from session ";
//$sql="select * from session where session_id='777'";
//$sql="select session_content from session where session_id='abcd'";
$this->queryarr($sql);
//$this->connect();
//$this->ready();
//echo "
".$this->cs['charset'];
//echo "";
//var_dump($this->arr);
//echo "
";
$this->tab($this->arr);
}
}