ホームページ  >  記事  >  バックエンド開発  >  mysqlデータベースを操作するための自作PHPクラスを共有する

mysqlデータベースを操作するための自作PHPクラスを共有する

WBOY
WBOYオリジナル
2016-07-25 08:48:56833ブラウズ
これは私自身が書いたクラスです。どなたでも議論したりガイドしたりできます。いくつかの使用法:
//データベース オブジェクトを宣言する
$Conn = new Mysql;ユーザー名、データベース パスワード、データベース名、データベース エンコーディング、データベース テーブル プレフィックス [空にすることもできます]);

上記のコードはすでにこのクラス ファイルをロードし、いくつかのデータベース接続パラメーターを初期化しています。
以下はいくつかの基本的なメソッド関数です:
1. $Conn -> Table();
データテーブルを選択します。パラメータはデータテーブルの名前です
2. $Conn -> Field();
フィールドを選択します。名前、複数 カンマで区切ります。このメソッドが呼び出されない場合は、すべてが返されます
3. $Conn -> Where();
Sql Where サブステートメント、条件に基づいてフィルターします
4. ;
SQL ソート
5 , $Conn -> Page (int);
パラメータは正の整数です。このメソッドが呼び出された場合、レコードはページ単位で表示されます
6. $Conn -> Select (ブール値) ;
クエリを実行し、クエリ結果を返します (存在しない場合は false を返します)。省略した場合、返される配列には数値要素が含まれます。 7、$Conn -> Del();
レコードを削除します
8、$ Conn -> Edit(array());
レコードを変更します。パラメータは 1 次元配列、配列キーはフィールド名です。配列の値はフィールドの値です
9 $Conn -> Into(array());
レコードを追加します。パラメータは 1 次元の配列、配列のキーはフィールド名、配列の値はフィールドです価値。

上記のメソッドは連続して呼び出すことができます。例:

$Rult = $Conn -> Table('user') -> Select(); //クエリはユーザー テーブル内のすべてのレコードを返します
$Rult = $ Conn -> Table( 'user') -> Page(20) -> Select();//クエリは user テーブル内のすべてのレコードを返し、それらをページごとに 20 レコードずつ表示します

;メソッド呼び出しや裏技もたくさんありますので、時間があるときに詳しく説明します。
  1. /*
  2. データベース操作クラス
  3. 操作方法: オブジェクトに面する
  4. 作成時期: 2013-3-10
  5. 現在のバージョン: BqhMysql (mysqli) 1.0.1
  6. ソフトウェア作者: Sad Song
  7. お問い合わせQQ: 36975
  8. 連絡先番号: 18907975647
  9. 電子メール: admin@q128.com
  10. 連絡先住所: 江西省贛州市石城県小松鎮
  11. 郵便番号: 342706
  12. */
  13. class Mysql{
  14. private $LocalHost = ' localhost';
  15. private $LoaclUser = 'root';
  16. private $LocalPass = '123456';
  17. private $LocalBase = 'jiangxibaiyi';
  18. private $LocalCode = 'UTF8';
  19. private $PreFix;
  20. private $Conn;
  21. private $Start = 0;
  22. private $Error = false; //データベースの接続ステータス、false は接続されていない、または接続が異常であることを意味します
  23. public $Err = true //SQL の実行結果
  24. private $Table;
  25. private $Field = '*';
  26. private $Where = '';
  27. private $Order = '';
  28. private $PageSize = 0; //ページ表示->ページごとのアイテム数、0 はページング表示なしを意味します
  29. private $ PageCount = 1; //ページ表示 -> 合計ページ数
  30. private $PageNum = 1; //ページ表示 -> 合計ページ数
  31. private $PageNo = 1; ->現在のページとは何ですか
  32. private $PageKey = 'page'; //ページ URL パラメーター キー
  33. private $PageStart = 0; //ページ表示->返される現在の項目
  34. private $Select;
  35. private $Rest;
  36. private $Result = false;//結果セット
  37. public $FormArray = array();
  38. public $Instr_ID = 0;
  39. private $j = 0;
  40. public function Parameter($ Loca、$Root、$Pass、$Base、$Code、$PreFix = ''){
  41. $this->LoaclUser = $Root;
  42. $this->LocalBase = $Base;
  43. $this->LocalCode = $Code;
  44. $this->LocalHost = $Loca;
  45. $this->LocalPass = $Pass;
  46. $this->PreFix = $PreFix;
  47. return $this;
  48. }
  49. プライベート関数 Connection( $ Sql ){
  50. !function_exists(mysqli_connect ) ? die('クエリが失敗しました。mysqli 拡張機能をロードできません') : null;
  51. $this->Conn = @new mysqli( $this->LocalHost, $this-> LoaclUser, $this->LocalPass, $this->LocalBase);
  52. $this->Error = mysqli_connect_errno() == 0 ? true : false;
  53. !$this->Error die('データベース接続 ?エラー。データベース接続パラメータを確認してください') : null;
  54. $this->Conn->query('SET NAMES ' . $this->LocalCode);
  55. $this->Rest = $this-> ;Conn->query($Sql) ;
  56. $this->Err = mysqli_error($this->Conn);
  57. $this->Instr_ID = mysqli_insert_id($this->Conn);
  58. $this ->Rest->free_result;
  59. $this->Conn->close;
  60. $this -> FormArray = '';
  61. return $this;
  62. }
  63. public function null(){
  64. $this ->PageSize = 0;
  65. // $this->PageCount = 1;
  66. $this->PageStart = 1;
  67. $this->フィールド = ' * ';
  68. $this->Select = ' ';
  69. unset($this->Table, $this->Where,$this->Order, $this->Result);
  70. }
  71. public function Table( $TableName ) {//データ テーブル
  72. $this -> null();
  73. $this->Table = '`' . $TableName . '`';
  74. return $this;
  75. }
  76. public function Field( $ Array = '*' ) {//data フィールド
  77. !empty( $this->Field ) ? $this->Field = '' : null;
  78. $Array =explode(',', $Array);
  79. foreach ( $Array as $field ) {
  80. $this->Field .= !$this->Start ? '`' : ', `' . $this->Start++;
  81. }
  82. $this->Start = 0;
  83. return $this;
  84. }
  85. public function Where( $Where ) {//Condition
  86. $this->Where = ' where ' .$Where;
  87. return $this; }
  88. public function Order( $Order ) {//Sort
  89. $this->Order = ' order by ' . $Order;
  90. return $this;
  91. }
  92. public function pk( $key ) {//ページング URL パラメーター key
  93. $this->PageKey = $key;
  94. return $this;
  95. }
  96. public function Page( $PageSize ) {///ページング
  97. $this-> PageSize = $PageSize;
  98. $this->gt;PageNo = $this->gt;get( $this->PageKey );
  99. $this->PageNo = empty( $this->PageNo ) || $this->PageNo ) || ! is_numeric( $this->PageNo 1 ? 1 : $this->PageNo;
  100. return $this;
  101. }
  102. function post( $Key, $Filter = true ){
  103. return $Filter ?strip_tags($_POST[$Key]) : $_POST[$Key];
  104. }
  105. public function get( $Key, $Filter = true ) {
  106. $Filter を返す ?strip_tags($_GET [$Key]) : $_GET[$Key];
  107. }
  108. パブリック関数 Sel(){
  109. $this->Select = 'Select ' 。 $this->フィールド 。 ' から ' 。 $this->テーブル 。 $this->どこ . $this->Order;
  110. $this->Connection( $this->Select );
  111. if ( $this->Rest->num_rows ) {
  112. while ( $Rs = $this->Rest ->fetch_assoc() ) {
  113. $this->Result[] = $Rs;
  114. }
  115. }
  116. $DataBase = $this->Result;
  117. return empty($DataBase) ? false : $DataBase;
  118. }
  119. public function querys( $Sql = '', $Type = 'not', $biao = false ) {
  120. $this->Select = $Sql;
  121. $this->Connection ( $this->Select );
  122. if ( $this->Rest->num_rows ) {
  123. if ( !$biao ) {
  124. while ( $Rs = $this->Rest->fetch_array() ) {
  125. $this->Result[] = !preg_match('/^d+$/i', $Type) ? $Rs : $Rs[ $Type ];
  126. }
  127. } else {
  128. while ( $Rs = $this->gt;Rest->fetch_assoc() ) {
  129. $this->Result[] = $Rs;
  130. }
  131. }
  132. }
  133. $DataBase = $this->Result;
  134. return empty($DataBase) ? false : $DataBase;
  135. }
  136. public functionexecutes( $Sql = '' ){
  137. $this->Connection( $Sql );
  138. return $this->Rest;
  139. }
  140. public 関数が存在します( $T = '', $F = '', $W = ''){
  141. if ( empty( $F ) ) { return 0; }
  142. $cmd = 空( $W ) ? ' sum(' . $F . ') を `baiyinum` として `' から選択します。 $this->PreFix 。 $T .'`' : 'sum(' . $F . ') を `baiyinum` として `' から選択します。 $this->PreFix 。 $T .'' ここで ' . $W;
  143. $this->Connection( $cmd );
  144. unset( $T, $F, $W, $cmd );
  145. $Rel = $this->>Rest->fetch_array();
  146. return Round( $Rel['baiyinum'], 2 );
  147. }
  148. public function ExistsTo( $Bili = 10000, $T = '', $F = '', $W = ''){
  149. if ( empty ( $F ) ) { 0 を返します。 }
  150. $cmd = 空( $W ) ? ' sum(' . $F . ') を `baiyinum` として `' から選択します。 $this->PreFix 。 $T .'`' : 'sum(' . $F . ') を `baiyinum` として `' から選択します。 $this->PreFix 。 $T .'' ここで ' . $W;
  151. $this->Connection( $cmd );
  152. unset( $T, $F, $W, $cmd );
  153. $Rel = $this->>Rest->fetch_array();
  154. return round( $Rel['baiyinum'] * $Bili );
  155. }
  156. public function Select( $Type = true, $ListNum = 1 ){ // 返记录(数组形式、返条数)
  157. $this- >選択 = '選択' 。 $this->フィールド 。 ' から ' 。 $this->テーブル 。 $this->どこ . $this->Order;
  158. if ( is_numeric( $ListNum ) ) {
  159. if ( $this->PageSize > 0 ) {
  160. $this->Connection( $this->Select );//実行查询
  161. $this->PageCount = $this->Rest->num_rows;//取得录总数
  162. $this->PageNum = ceil($this->PageCount / $this->PageSize); //共通共有量
  163. $this->PageNo = $this->PageNo > $this->ページ番号 ? $this->PageNum : $this->PageNo;
  164. $this->PageStart = ( $this->PageNo - 1 ) * $this->PageSize; //当前从第几条开始返
  165. $this->Select .= 'limit' 。 $this->PageStart 。 ', ' .$this->PageSize; // 重新构造句sql语
  166. } else {
  167. $this->Select .= 'limit' . $ListNum; // 重新构造句sql语
  168. }
  169. } else {
  170. $this->Select .= ' 制限 1'; // 重新构造句sql语
  171. }
  172. //echo $this->Select;
  173. $this->Connection( $this->Select );// 再実行查询
  174. if ( $this->Rest- >num_rows ) {//如果记录存在
  175. if ( $Type ) {
  176. while ( $Rs = $this->Rest->fetch_array() ) {
  177. $this->Result[] = $Rs;
  178. }
  179. }else{
  180. while ( $Rs = $this->Rest->fetch_assoc() ) {
  181. $this->Result[] = $Rs;
  182. }
  183. }
  184. }
  185. if ( ( $ ListNum == 1 または !is_numeric( $ListNum ) ) && !$this->PageSize) { $this->Result = $this->Result[0]; }
  186. $DataBase = $this->Result;
  187. return empty($DataBase) ? false : $DataBase;
  188. }
  189. public function Num() { // 戻り记录总数
  190. $this->Select = 'Select ' . $this->フィールド 。 ' から ' 。 $this->テーブル 。 $this->どこ . $this->Order;
  191. $this->Connection( $this->Select );//実行查询
  192. return $this->Rest->num_rows;// 取得记录总数
  193. }
  194. public function PageNav($NumNav = false ) { //分页
  195. $Action = $this -> get('action');
  196. !empty( $Action ) or $Action = 'index';
  197. $Module = $this -> get('module');
  198. !empty( $Module ) or $Module = 'index';
  199. $NavUrl = '/' . $Module . '/' . $Action . '/' . $this -> PageKey .'/';
  200. $NaIndex = '/' . $Module . '/' . $Action;
  201. $PageHtml = "\n
    ";
  202. $PageHtml .= '' . $this->PageCount . '条记录 ' . $this->PageNo . '/' . $this->PageNum . '页 ';
  203. $this->PageNo <= 1 or $PageHtml .= "首页\nPageNo - 1) . "\">上一页\n";
  204. if ( $NumNav ) { $PageHtml .= $this->NumPage($NavUrl); }
  205. $this->PageNo >= $this->PageNum or $PageHtml .= "PageNo + 1) . "\">下一页\nPageNum . "\">尾页\n";
  206. $PageHtml .= "
\n";
  • return $PageHtml;
  • }
  • private function NumPage( $Can = '' ) { //数字分页
  • $NumHtml = '';
  • $First = 1;
  • $Last = $this->PageNum;
  • if ( $this->PageNum > 5 ) {
  • if ( $this->PageNo < $this->PageNum ) {
  • $First = $this->PageNo - 2;
  • $Last = $this->PageNo + 2;
  • }else{
  • $First = $this->PageNo - 4;
  • $Last = $this->PageNum;
  • }
  • }
  • if ( $First < 1 ) { $First = 1; $Last = $First + 4;}
  • if ( $Last > $this->PageNum ) { $First = $this->PageNum - 4; $Last = $this->PageNum;}
  • for( $i = $First; $i <= $Last; $i++) {
  • $NumHtml .= $this->PageNo != $i ? "\n\t" . '' . $i . '' . "\n\t" : "\n\t" .'' . $i . '' . "\n\t";
  • }
  • unset($Can, $First, $i, $Last);
  • return $NumHtml;
  • }
  • public function UserPage($NumNav = false, $PageName = 'index', $Mulu = 'user' ) { //会员中心分页
  • $NavUrl = '/' . $Mulu . '/' . $PageName . '/' . $this->PageKey . '/';
  • $PageHtml = "\n
    ";
  • $PageHtml .= '' . $this->PageCount . '条记录 ' . $this->PageNo . '/' . $this->PageNum . '页 ';
  • $this->PageNo <= 1 or $PageHtml .= "首页\nPageNo - 1) . "\">上一页\n";
  • if ( $NumNav ) { $PageHtml .= $this->NumPage($NavUrl); }
  • $this->PageNo >= $this->PageNum or $PageHtml .= "PageNo + 1) . "\">下一页\nPageNum . "\">尾页\n";
  • $PageHtml .= "
  • n";
  • return $PageHtml;
  • }
  • // フォーム処理開始
  • // フォーム判定時に送信
  • public function FormIs( $Keys = 'mm' ) {
  • return $_POST[ $Keys ] == 1 ? true : false;
  • }
  • //post 経由でデータを取得
  • public function _post( $Keys = '', $TiHuan = '') {
  • $Values =trip_tags( $_POST[ $Keys ] );
  • $ this->FormArray[$Keys] = empty( $Values ) ? $TiHuan : $Values;
  • return empty( $Values ) ? $TiHuan : $Values;
  • }
  • //メソッドを取得データを取得するには
  • public function _get ( $Keys = '', $TiHuan = '') {
  • $Values =trip_tags( $_GET[ $Keys ] );
  • return empty( $Values ) ? $Values;
  • }
  • //数値か0以上かを判定
  • public function IsNum( $Num = 0, $Mesg = 'パラメータは数値でなければなりません' ) {
  • if ( is_numeric( $Num ) && !empty( $Num ) && $Num >= 0 ) {
  • return $Num;
  • }else{
  • die( $Mesg );
  • }
  • }
  • //それが数値であり、0以上であるかどうかを判断しますそして return True/False
  • public function NumBer( $Num = 0) {
  • return is_numeric( $Num ) && !empty( $Num ) && $Num >= 0 ? true : false;
  • }
  • //それを検出します関連するデータが存在するようです
  • public function IsData($Types = true, $memg = 'データはすでに存在します' ){
  • $this->Connection('select ' . $this->Field . ' from ' . $this ->Table . $this->Where);
  • if ( $Types ){
  • $this->Rest->num_rows > die( $memg ) : null;
  • } else {
  • return $ this->Rest->num_rows;
  • }
  • }
  • //データベースレコードに書き込む
  • public function into( $Mesg = '' ){
  • !is_array( $this->FormArray ) ? Mesg ) : null;
  • $Sql = ' に挿入 . $this->Table . ' (`';
  • $I = 0;
  • foreach ( $this->FormArray as $Key => $Val ) {
  • $Duan .= !$I ? $Key . '`' : ', ` ' . '`';
  • if ( is_numeric( $Val ) ){
  • $Vals .= !$I ? : ', ' . $Val;
  • }else{
  • $Val .= !$I ? '' : ', '' . $Val .
  • }
  • $Sql .= $Duan . ') 値 (' . $Vals . ' )';
  • //@file_put_contents('1.sql', $Sql, FILE_APPEND);
  • $this-> ;Connection( $Sql );
  • return !empty( $this->Err ) ? false : true;
  • }
  • //データを配列形式で書き込む
  • public function MsgBox( $Table = '', $Filed = array () ) {
  • $this -> Table($Table);
  • foreach( $Filed as $Key => $Val ) {
  • $this -> FormArray[ $Key ] = $Val; $this -> Into('データが取得されません');
  • }
  • //データベース Record を変更します
  • public function Edit( $Array = array() ) {
  • if ( empty( $Array ) ) { $Array = $this -> }
  • if ( !is_array( $Array ) || empty( $Array ) ) {
  • return false;
  • } else {
  • $Sql = 'update ' . set ';
  • $I = 0;
  • $Sub = '';
  • $Huan = array(' -' => '[jian]', '+' => '[jia]', '*' = > '[チェン]', '/' => '[チュウ]');
  • $ ザン = array('[ジャン]' => '-', '[ジア]' => '+' , '[cheng]' => '*', '[chu]' => '/ ');
  • foreach ( $Array as $Files => $Val ) {
  • $Val = !is_numeric( $ Val ) && !preg_match('/`w+`s*(+|-|*|/)/ i', $Val) '' . $Val;
  • foreach ( $Huan as $key => $val ){
  • $Val = str_replace($key, $val, $Val );
  • $duan = '`' . $Files . ' . $Files . '' = ';
  • $Sub .= $duan . $Val;
  • $ I++;
  • }
  • $Sub . = $Fan ; => $Hui ) {
  • $Sql = str_replace($Fan, $Hui, $Sql);
  • }
  • //echo $Sql;
  • $this -> 接続を解除( $Array, $duan, $Fan, $Files, $Huan, $Hui, $I, $ key, $Sql, $Sub, $Val, $Zhan, $val );
  • return !empty( $this -> ; エラー) ? false : true;
  • }
  • // データベース レコードを削除します
  • $Sql = '$this->Table . $this->Connection( $Sql );
  • unset($Sql);
  • return !empty( $this->Err ) ? false : true;
  • }
  • //フォーム処理が終了します
  • //ページジャンプ
  • public function Msg( $Text = 'Operation success' ) {
  • echo '';
  • echo ' ';
  • exit;
  • }
  • #現在のシステム時刻を取得します
  • public function Times(){
  • return str_replace('-', '[jian]', date ('Y-m-d H:i:s'));
  • }
  • # 取得用户IP地址
  • public function GetIP(){
  • if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  • $ip = getenv("HTTP_CLIENT_IP");
  • else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
  • $ip = getenv("HTTP_X_FORWARDED_FOR");
  • else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  • $ip = getenv("REMOTE_ADDR");
  • else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
  • $ip = $_SERVER['REMOTE_ADDR'];
  • else
  • $ip = "不明";
  • return($ip);
  • }
  • //最後关闭データベース库连接
  • public function Close(){
  • !is_object( $this -> Conn ) または mysqli_close( $this -> Conn );
  • }
  • }
  • 复制定


    声明:
    この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。