輕言放棄之博客005

引入關系:init.php > mysql.php > config.php

// init.php
<?php 
    
    // 魔術常量不受require,include的影響
    // echo __DIR__, "<br/>";// C:\xampp\htdocs\QYFQ\lib
    // echo dirname(__DIR__), "<br/>";// 當前目錄的上一級
    // echo __FILE__, "<br/>";// C:\xampp\htdocs\QYFQ\lib\init.php
    // echo __LINE__;// 5
    
    // 字符集
    header('Content-type: text/html; charset=utf8');
    define('ROOT', dirname(__DIR__));

    // echo ROOT;
    require(ROOT.'/lib/mysql.php');

 ?>
// mysql.php
<meta charset="utf8">
<?php 
    /**
     * @return resource 連接成功,返回資源
     */
    function mConn() {
        // $cfg = require('./config.php');// 當被另一個目錄引用后當前目錄就變成了引用的目錄,誰引用誰就是當前目錄
        // $cfg = require('./lib/config.php');
        $cfg = require(ROOT.'/lib/config.php');

        static $conn = null;
        if($conn == null) {// 說明第一次調用
            $conn = mysql_connect($cfg['host'], $cfg['user'], $cfg['pwd']);
            mysql_query('use '.$cfg['db'], $conn);
            mysql_query('set names '.$cfg['charset'], $conn);
        }
        return $conn;
    }
    /**
     * 查詢的函數
     * @return mixed resource/Boolean
     */
    function mQuery($sql) {
        return mysql_query($sql, mConn());
    }

    /**
     * select 查詢多行數據
     * @param  str $sql select 待查詢的sql語句
     * @return mixed select 查詢成功返回一個二維數組,失敗返回Boolean false
     */
    function mGetAll($sql) {
        $rs = mQuery($sql);
        if( !$rs ) {// false
            return false;
        }

        $data = array();
        while( $row=mysql_fetch_assoc($rs) ) {
            $data[] = $row;
        }
        return $data;
    }

    // $sql = "select * from cat";
    // print_r( mGetAll($sql) );
    

    /**
     * select 取出一行數據
     * @param str $sql 待查詢的sql語句
     * @return arr/false 查詢成功 返回一個一維數組
     */
    function mGetRow($sql) {
        $rs = mQuery($sql);
        if( !$rs ) {
            return false;
        }
        return mysql_fetch_assoc($rs);// 返回一行的數據就不用循環了
    }
    
    // $sql = "select * from cat where cat_id=3";
    // print_r(mGetRow($sql));
    
    /**
     * select 查詢返回一個結果
     * @param str $sql 待查詢的select語句
     * @return mixed 成功,返回結果,失敗返回Boolean false
     */
    function mGetOne($sql) {
        $rs = mQuery($sql);
        if( !$rs ) {// false
            return false;
        }
        return mysql_fetch_row($rs)[0];
    }

    // $sql = "select count(*) from art where cat_id=2";// cat_id 類目
    // print_r(mGetOne($sql));
    

    // insert into cat (id, catname) values ('6', 'test')
    /**
     * 自動拼接insert 和 update sql語句,并且調用mQuery()去執行 sql
     * @param str $table 表名
     * @param arr $data 接收到的數據是一維數組
     * @param str $act 動作 默認為insert
     * @param str $where 防止update更改時少加where條件
     * @return Boolean insert or update success or faild
     */
    function mExec($table, $data, $act='insert', $where=0) {
        if($act == 'insert') {
            // implode 數組轉字符串
            $sql = "insert into $table(";
            $sql .= implode(',', array_keys($data)) . ") values ('";
            $sql .= implode("','",array_values($data)) . "')";
            return mQuery($sql);
        }
        else if($act == "update") {
            $sql = "update $table set ";
            foreach($data as $k => $v) {
                $sql .= $k . "='" .$v . "',";
            }
            $sql = rtrim($sql, ',') . " where " . $where;// 去掉最后一個多余的逗號
            // echo $sql;
            return mQuery($sql);
        }
    }

    $data = array('title' => '今天的空氣', 'content' => '空氣質量堪憂', 'pubtime' => 123456, 'author' => 'Aaayang');
    // insert into art(title, content, pubtime, author) values ('今天的空氣', '空氣質量堪憂', '123456', 'Aaayang');
    // update art set title='今天的空氣', content='空氣質量堪憂', pubtime='123456', author='Aaayang' where art_id=1;
    // echo mExec('art', $data, 'update', "art_id=1");

    /**
     * 取得上一步insert操作產生的主鍵ID
     */
    function getLastId() {
        return mysql_insert_id(mConn());
    }
    
 ?>
// config.php
<?php 

    return array(
        'host' => 'localhost',
        'user' => 'root',
        'pwd' => '',
        'db' => 'blog',
        'charset' => 'utf8'
    );

 ?>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • 自己做dede的開發時間也比較長了,基本上常用的函數都知道在哪個文件里面,但是時間一長,也有點模糊了,俗話說:好記...
    大劉的英語世界閱讀 1,725評論 1 9
  • 更改ip和dnsVi /etc/sysconfig/network-scripts/ifcfg-eth0vi /...
    Xwei_閱讀 1,846評論 0 3
  • Php:腳本語言,網站建設,服務器端運行 PHP定義:一種服務器端的HTML腳本/編程語言,是一種簡單的、面向對象...
    廖馬兒閱讀 2,167評論 2 38
  • 童年 小時候 爸爸打我的時候 烏云總是布滿了頭上的天空 我的媽媽被悲傷籠罩 我一次次懊惱 悔恨 掙扎 一片片拾起 ...
    卻悔閱讀 176評論 0 4