引入關系: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'
);
?>