php mysql 小项目_一个小工程的准备工作 - Php MySql Class

我比较喜欢的一个。

/**

* 数据库操作类

*

* @author Moyo

* @package defaultPackage

*/

class Mysql

{

// 默认配置

private $_config_default = array

(

'debug' => false,

'host' => 'localhost:3306',

'username' => 'root',

'password' => '',

'database' => 'mysql',

'prefix' => '',

'charset' => 'utf-8',

'cached' => 'file://{root}/query_cache/'

);

public $CACHE_HASH_SALT = 'sql.cache.uuland.org';

public $CLIENT_MULTI_RESULTS = 131072;

// 配置信息

private $_config = array();

private $_debug = true;

private $_host = '';

private $_username = '';

private $_password = '';

private $_database = '';

private $_prefix = '';

private $_charset = '';

private $_cached = '';

private $_fc_path = '';

private $_mc_server = '';

// 运行时变量

private $_dbc_handle = null;

private $_query_handle = null;

public $sql = '';

private $_cache_key = '';

private $_result = array();

private $_need_cache = false;

// 数据库操作

private $_operate = '';

private $_column = '';

private $_where = array();

private $_order = array();

private $_limit = '';

private $_data = array();

private $_cache = '';

// 调试记录

private $_trace = array();

// 获取实例

public function getInstance()

{

return new self();

}

// 私有化构造函数,禁止外部实例化

private function __construct(){}

// 卸载实例时,自动释放资源,关闭连接

public function __destruct()

{

// 释放资源

$this->free();

// 关闭连接

$this->close();

}

// 载入配置

public function config($config)

{

$this->trace('public::config::load');

$this->_config = $config;

// 执行初始化

$this->init();

}

// 初始化

private function init()

{

$this->trace('private::config::init_default');

// 配置分析

foreach ($this->_config as $key => $val)

{

$mkey = '_'.$key;

$this->$mkey = isset($this->_config[$key]) ? $this->_config[$key] : $this->_config_default[$key];

}

// 清理非debug模式下,之前记录的调试信息

if (!$this->_debug) unset($this->_trace);

// 缓存配置

$this->trace('private::config::init_cache');

$cache_conf = explode('://', $this->_cached);

$this->_cached = $cache_conf[0];

if ($this->_cached == 'file')

{

$this->_fc_path = str_replace('{current}', dirname(__FILE__), str_replace('{root}', $_SERVER['DOCUMENT_ROOT'], $cache_conf[1]));

// 检测目录

if (!is_dir($this->_fc_path))

{

mkdir($this->_fc_path);

}

}

elseif ($this->_cached == 'memcache')

{

$this->_mc_server = $cache_conf[1];

}

unset($this->_config);

}

// 连接至数据库

private function connect()

{

$this->trace('public::server::connect');

// 连接服务器

$this->_dbc_handle = mysql_connect(

$this->_host,

$this->_username,

$this->_password,

true,

$this->CLIENT_MULTI_RESULTS

);

if (!$this->_dbc_handle)

{

$this->alert('Can\'t connect to Server [ '.$this->_username.'@'.$this->_host.' ]');

return false;

}

// 选择数据库

if (!mysql_select_db($this->_database, $this->_dbc_handle))

{

$this->alert('Can\'t select database ['.$this->_database.']');

return false;

}

$version = mysql_get_server_info($this->_dbc_handle);

// 设置数据库编码

if ($version >= '4.1')

{

//使用UTF8存取数据库 需要mysql 4.1.0以上支持

mysql_query('SET NAMES "'.$this->_charset.'"', $this->_dbc_handle);

}

//设置 sql_model

if($version > '5.0.1')

{

mysql_query('SET SQL_Mode=""', $this->_dbc_handle);

}

return true;

}

// 释放数据查询

private function free()

{

$this->trace('public::query::free');

if ($this->_query_handle && $this->_operate == 'SELECT')

{

mysql_free_result($this->_query_handle);

}

unset($this->_query_handle);

unset($this->_operate);

unset($this->_column);

unset($this->_where);

unset($this->_order);

unset($this->_limit);

unset($this->_data);

unset($this->_cache);

unset($this->_result);

return true;

}

// 关闭数据库连接

private function close()

{

if ($this->_dbc_handle)

{

$this->trace('public::server::close');

mysql_close($this->_dbc_handle);

unset($this->_dbc_handle);

}

}

//

// 增改删查

public function select($column)

{

$this->_operate = 'SELECT';

$this->_column = $column;

return $this;

}

public function update($column)

{

$this->_operate = 'UPDATE';

$this->_column = $column;

return $this;

}

public function insert($column)

{

$this->_operate = 'INSERT';

$this->_column = $column;

return $this;

}

public function delete($column)

{

$this->_operate = 'DELETE';

$this->_column = $column;

return $this;

}

// 条件

public function where($where)

{

$this->_where[] = $where;

return $this;

}

// 排序

public function order($order)

{

$this->_order[] = $order;

return $this;

}

// 限制返回结果数

public function limit($limit)

{

$this->_limit = $limit;

return $this;

}

// 数据存储

public function data($data)

{

$this->_data[] = $data;

return $this;

}

// 缓存设置

public function cache($cache)

{

$this->_cache = $cache;

return $this;

}

// 开始执行操作

public function done()

{

$this->trace('public::query::init');

// 数据表

$column = $this->_prefix.$this->_column;

// 组合SQL

switch ($this->_operate)

{

case 'SELECT':

$sql = 'SELECT * FROM `'.$column.'`'.$this->pack_where().$this->pack_order().$this->pack_limit();

break;

case 'UPDATE':

$sql = 'UPDATE `'.$column.'`'.$this->pack_data().$this->pack_where();

break;

case 'INSERT':

$sql = 'INSERT INTO `'.$column.'`'.$this->pack_data();

break;

case 'DELETE':

$sql = 'DELETE FROM `'.$column.'`'.$this->pack_where();

break;

default: break;

}

$this->sql = $sql;

// 缓存判断 [暂时只支持缓存查询]

if ($this->_operate == 'SELECT' && $this->cache_check())

{

$return = $this->_result;

// 清理变量池并返回

if ($this->free()) return $return;

}

// 连接判断

if (!$this->_dbc_handle) $this->connect();

// 开始执行SQL

$this->trace('public::query::begin['.$this->_operate.']');

$this->_query_handle = mysql_query($sql, $this->_dbc_handle);

if (!$this->_query_handle)

{

$this->alert('SQL run error.');

}

$this->trace('public::query::finish['.$this->_operate.']');

if ($this->_operate == 'SELECT')

{

if (mysql_num_rows($this->_query_handle) > 0)

{

while ($one_row = mysql_fetch_assoc($this->_query_handle))

{

$this->_result[] = $one_row;

}

mysql_data_seek($this->_query_handle, 0);

}

else

{

$this->_result = null;

}

// 写缓存

if ($this->_need_cache) $this->cache_write();

$return = $this->_result;

// 清理变量池并返回

if ($this->free()) return $return;

}

else

{

$return = mysql_affected_rows($this->_dbc_handle);

// 清理变量池并返回

if ($this->free()) return $return;

}

}

// 返回结果限制

private function pack_limit()

{

if ($this->_limit == '') return '';

if (is_numeric($this->_limit))

{

return ' LIMIT 0,'.$this->_limit;

}

elseif (is_string($this->_limit))

{

return ' LIMIT '.$this->_limit;

}

}

// 条件整合

private function pack_where()

{

if (!$this->_where) return '';

$sql_where = ' WHERE ';

foreach ($this->_where as $where)

{

if (is_array($where))

{

foreach ($where as $key => $val)

{

if (is_numeric($val))

{

$sql_where .= $key.'='.$val;

}

elseif (is_string($val))

{

$sql_where .= $key.'="'.$val.'"';

}

$sql_where .= ' and ';

}

}

elseif (is_string($where))

{

$conds = explode(',', $where);

foreach ($conds as $one_cond)

{

$sql_where .= $one_cond.' and ';

}

}

}

return substr($sql_where, 0, -5);

}

// 排序整合

private function pack_order()

{

if (!$this->_order) return '';

$sql_order = ' ORDER BY ';

foreach ($this->_order as $order)

{

if (is_array($order))

{

foreach ($order as $key => $type)

{

$sql_order .= $key.' '.$type.', ';

}

}

elseif (is_string($order))

{

$ords = explode(',', $order);

foreach ($ords as $one_ord)

{

$sql_order .= str_replace('.', ' ', $one_ord).', ';

}

}

}

return substr($sql_order, 0, -2);

}

// 数据整合

private function pack_data()

{

if (!$this->_data) return '';

$sql_data = ' SET ';

foreach ($this->_data as $data)

{

if (is_array($data))

{

foreach ($data as $key => $val)

{

if (is_numeric($val))

{

$sql_data .= $key.'='.$val;

}

elseif (is_string($val))

{

$sql_data .= $key.'="'.$val.'"';

}

$sql_data .= ', ';

}

}

elseif (is_string($data))

{

$datas = explode(',', $data);

foreach ($datas as $one_data)

{

$sql_data .= $one_data.', ';

}

}

}

return substr($sql_data, 0, -2);

}

// ]>

// 缓存检测

private function cache_check()

{

$this->trace('private::cache::check');

if ($this->_cache == '') return false;

$this->_cache_key = md5($this->sql.'@'.$this->CACHE_HASH_SALT);

$time_calc = array

(

's' => 1,

'm' => 60,

'h' => 3600,

'd' => 86400

);

$c_rule = explode(':', $this->_cache);

$c_time = $c_rule[0];

$c_long = (int)$c_rule[1];

if(time() - $this->cache_time() > $time_calc[$c_time]*$c_long)

{

$this->_need_cache = true;

return false;

}

$this->_result = $this->cache_read();

return true;

}

// 获取时间

private function cache_time()

{

$handle = 'cache_handle_'.$this->_cached.'_time';

return $this->$handle($this->_cache_key);

}

// 读缓存

private function cache_read()

{

$this->trace('private::cache::read');

$handle = 'cache_handle_'.$this->_cached.'_value';

return $this->$handle($this->_cache_key);

}

// 写缓存

private function cache_write()

{

$this->trace('private::cache::write');

$handle = 'cache_handle_'.$this->_cached.'_write';

$this->$handle($this->_cache_key, $this->_result);

$this->_need_cache = false;

}

//

// 文件缓存

private function cache_handle_file_time($key)

{

if (is_file($this->_fc_path.$key.'.sql'))

{

return filemtime($this->_fc_path.$key.'.sql');

}

else

{

return 0;

}

}

private function cache_handle_file_value($key)

{

if (is_file($this->_fc_path.$key.'.sql'))

{

return unserialize(file_get_contents($this->_fc_path.$key.'.sql'));

}

else

{

return false;

}

}

private function cache_handle_file_write($key, $val)

{

file_put_contents($this->_fc_path.$key.'.sql', serialize($val));

return true;

}

// memcache 缓存 [这里做的不怎么好,不过平常也用不到memcache的 ^_^]

private function cache_handle_memcache_time($key)

{

$mec = new Memcache();

$mec->connect($this->_mc_server);

$val = $mec->get($this->_cache_key.'_time');

$mec->close();

if ($val == '')

{

return 0;

}

else

{

return $val;

}

}

private function cache_handle_memcache_value($key)

{

$mec = new Memcache();

$mec->connect($this->_mc_server);

$val = $mec->get($this->_cache_key.'_value');

$mec->close();

if ($val == '')

{

return false;

}

else

{

return $val['value'];

}

}

private function cache_handle_memcache_write($key, $val)

{

$mec = new Memcache();

$mec->connect($this->_mc_server);

$mec->set($this->_cache_key.'_time', time());

$mec->set($this->_cache_key.'_value', array('cached'=>true,'value'=>$val));

$mec->close();

return true;

}

// ]>

//

// 功能后续添加

// ]>

// 调试信息

private function alert($message)

{

if (!$this->_debug) return;

echo '

';

echo $message;

echo '


';

echo mysql_error();

echo '

';

exit;

}

// 记录调试

private function trace($message)

{

if (!$this->_debug) return;

$this->_trace[] = array('timer'=>microtime(), 'mmusage'=>memory_get_usage(), 'message'=>$message);

}

// 输出调试

public function trace_output()

{

if (!$this->_debug) return;

echo '

';

echo '

  • ';

foreach ($this->_trace as $i => $trace)

{

$timer_e = explode(' ', $trace['timer']);

$timer = (float)$timer_e[0];

$mmusage = $trace['mmusage'];

echo '

Time: '.$timer.' +'.($timer-$last_timer).' Memory: '.$trace['mmusage'].' +'.($mmusage-$last_mmusage).' Call: '.$trace['message'].'';

$last_timer = $timer;

$last_mmusage = $mmusage;

}

echo '

';

echo '

';

}

}

?>

具体使用方法:

include 'class.mysql.php';

echo '';

$conf_info = array

(

// 开启调试

'debug'=>true,

// MySQL主机

'host'=>'localhost:3306',

// 用户

'username'=>'run',

// 密码

'password'=>'moyo',

// 数据库

'database'=>'test',

// 数据表前缀

'prefix' => '',

// 数据库编码

'charset'=>'utf8',

// 缓存方式

// memcache缓存协议,“://” 后面的是服务器地址

//'cached'=>'memcache://127.0.0.1:11211'

// 文本缓存协议,“://”后面的是缓存地址。可用的标记:{root} 站点根目录, {current} 当前脚本目录

'cached'=>'file://{current}/query_cache/'

);

// 获取实例

$dbc = db_mysql::getInstance();

// 载入配置 [只支持数组方式]

$dbc->config($conf_info);

$start = explode(' ', microtime());

$memory_start = memory_get_usage();

// 插入数据

$affect = $dbc

// 操作表:user

->insert('user')

// 支持数组方式

->data(array('name'=>"Moyo"))

// 支持字符方式

->data('mail="moyo@mail"')

->done();

echo $dbc->sql;

echo '

echo 'INSERT 操作完成,影响行数:'.$affect;

echo '


';

// 修改数据

$affect = $dbc

->update('user')

// 支持数组方式

->where(array('name'=>'Moyo'))

// 支持字符方式

->where('mail="moyo@mail"')

->data(array('name'=>"Moyo.live", 'mail'=>'moyo@uuland'))

->done();

echo $dbc->sql;

echo '

echo 'UPDATE 操作完成,影响行数:'.$affect;

echo '


';

// 获取数据

$result = $dbc

->select('user')

->where('name like "%Moyo%"')

->order('id.desc')

->limit(3)

// 使用缓存,有效时间:10秒 [单位支持:d 天,h 时, m 分, s 秒]

->cache('s:10')

->done();

echo $dbc->sql;

echo '

echo 'SELECT 完成,记录数:'.count($result);

echo '

';

print_r($result);

echo '

';

echo '


';

// 删除数据

$affect = $dbc

->delete('user')

->where('name="Moyo.live"')

->done();

echo $dbc->sql;

echo '

echo 'DELETE 操作完成,影响行数:'.$affect;

echo '


';

$finish = explode(' ', microtime());

$memory_finish = memory_get_usage();

$start = $start[1]+$start[0];

$finish = $finish[1]+$finish[0];

$time = $finish-$start;

$memory = $memory_finish-$memory_start;

echo 'Trace (Time use '.$time.' Sec , Memory use '.$memory_finish.' Bytes , Incress '.$memory.' Bytes)';

echo '


';

$dbc->trace_output();

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
连接 MySQL 数据库需要使用服务器端语言,比如 PHP、Python、Java 等,以及相应的数据库连接库。这里以 PHP 和 PDO 为例来介绍如何连接 MySQL 数据库,并使用 Vue.js 和 MVC 架构进行前端开发。 1. 安装 PHPMySQL 首先需要安装 PHPMySQL,具体安装方式可以自行搜索。 2. 创建数据库 在 MySQL 中创建一个数据库,并创建一个表,用于存储数据。以下是创建表的 SQL 语句: ``` CREATE TABLE `books` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `author` varchar(255) NOT NULL, `price` decimal(10,2) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ``` 3. 编写 PHP 文件连接数据库 在 PHP 中连接 MySQL 数据库可以使用 PDO,以下是一个示例代码: ```php <?php // 连接数据库 $dsn = "mysql:host=localhost;dbname=database_name"; $username = "username"; $password = "password"; try { $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "数据库连接失败:".$e->getMessage(); exit; } ?> ``` 其中,`$dsn` 变量是数据库连接字符串,包含数据库类型、主机名、数据库名等信息。`$username` 和 `$password` 变量是数据库用户名和密码。 4. 编写 PHP 文件获取数据 在 PHP 中获取数据库中的数据可以使用 PDO 的 `query` 方法,例如: ```php <?php // 获取数据 $sql = "SELECT * FROM books"; $stmt = $pdo->query($sql); $books = $stmt->fetchAll(PDO::FETCH_ASSOC); // 输出数据 header('Content-Type: application/json'); echo json_encode($books); ?> ``` 其中,`$sql` 变量是 SQL 查询语句,`$stmt` 变量是 PDOStatement 对象,`$books` 变量是一个包含所有数据的数组。最后,使用 `json_encode` 函数将数组转换成 JSON 格式输出。 5. 编写 Vue.js 组件 使用 Vue.js 开发前端页面,可以将页面拆分成多个组件,每个组件都是一个独立的 Vue 实例。以下是一个获取数据并显示在页面上的 Vue 组件示例: ```html <template> <div> <h1>图书列表</h1> <table> <thead> <tr> <th>编号</th> <th>书名</th> <th>作者</th> <th>价格</th> </tr> </thead> <tbody> <tr v-for="book in books" :key="book.id"> <td>{{ book.id }}</td> <td>{{ book.title }}</td> <td>{{ book.author }}</td> <td>{{ book.price }}</td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { books: [] } }, mounted() { this.fetchData(); }, methods: { fetchData() { fetch('/api/books.php') .then(response => response.json()) .then(data => this.books = data) .catch(error => console.error(error)); } } } </script> ``` 其中,`fetchData` 方法使用 `fetch` 函数向服务器发起请求,并将返回的 JSON 数据赋值给组件的 `books` 属性。`v-for` 指令用于循环遍历 `books` 数组,`v-bind` 指令用于将数据绑定到 HTML 元素上。 6. 编写 MVC 控制器 在 MVC 架构中,控制器负责处理用户请求并返回响应。以下是一个简单的控制器示例,用于返回图书列表数据: ```php <?php class BookController { public function actionList() { // 获取数据 $model = new BookModel(); $books = $model->getAll(); // 渲染视图 $view = new View(); $view->render('book/list', ['books' => $books]); } } ?> ``` 其中,`BookModel` 是一个模型类,用于操作数据库并获取数据。`View` 是一个视图类,用于渲染 HTML 页面并将数据传递给页面。 7. 编写 MVC 模型 在 MVC 架构中,模型负责处理数据,例如从数据库中获取数据。以下是一个简单的模型示例,用于从数据库中获取图书列表数据: ```php <?php class BookModel { public function getAll() { // 连接数据库 $dsn = "mysql:host=localhost;dbname=database_name"; $username = "username"; $password = "password"; $pdo = new PDO($dsn, $username, $password); // 获取数据 $sql = "SELECT * FROM books"; $stmt = $pdo->query($sql); $books = $stmt->fetchAll(PDO::FETCH_ASSOC); return $books; } } ?> ``` 8. 编写 MVC 视图 在 MVC 架构中,视图负责渲染 HTML 页面并将数据传递给页面。以下是一个简单的视图示例,用于显示图书列表数据: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书列表</title> </head> <body> <h1>图书列表</h1> <table> <thead> <tr> <th>编号</th> <th>书名</th> <th>作者</th> <th>价格</th> </tr> </thead> <tbody> <?php foreach ($books as $book) { ?> <tr> <td><?php echo $book['id']; ?></td> <td><?php echo $book['title']; ?></td> <td><?php echo $book['author']; ?></td> <td><?php echo $book['price']; ?></td> </tr> <?php } ?> </tbody> </table> </body> </html> ``` 其中,`$books` 变量是从控制器传递过来的数据,使用 PHP 的 `foreach` 循环遍历数据并将数据显示在 HTML 页面上。 9. 整合前后端代码 最后,将前端代码和后端代码进行整合,并将 PHP 文件放在服务器上运行即可。以下是一个简单的目录结构示例: ``` project/ ├── api/ │ └── books.php ├── controllers/ │ └── BookController.php ├── models/ │ └── BookModel.php ├── views/ │ └── book/ │ └── list.php └── public/ ├── index.html └── js/ └── app.js ``` 其中,`api` 目录存放 PHP 文件,用于处理 AJAX 请求;`controllers` 目录存放控制器类;`models` 目录存放模型类;`views` 目录存放视图文件;`public` 目录存放前端代码,包括 HTML、CSS、JS 等文件。 在 `api/books.php` 文件中,使用 `BookController` 类处理请求,并返回 JSON 格式的数据: ```php <?php require_once '../controllers/BookController.php'; $controller = new BookController(); $data = $controller->actionList(); header('Content-Type: application/json'); echo json_encode($data); ?> ``` 在 `public/index.html` 文件中,引入 Vue.js 和 app.js 文件,并使用 `BookList` 组件显示图书列表: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书列表</title> </head> <body> <div id="app"> <book-list></book-list> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="js/app.js"></script> </body> </html> ``` 在 `public/js/app.js` 文件中,定义 `BookList` 组件并使用 `fetch` 函数获取数据: ```js Vue.component('book-list', { template: ` <div> <h1>图书列表</h1> <table> <thead> <tr> <th>编号</th> <th>书名</th> <th>作者</th> <th>价格</th> </tr> </thead> <tbody> <tr v-for="book in books" :key="book.id"> <td>{{ book.id }}</td> <td>{{ book.title }}</td> <td>{{ book.author }}</td> <td>{{ book.price }}</td> </tr> </tbody> </table> </div> `, data() { return { books: [] } }, mounted() { this.fetchData(); }, methods: { fetchData() { fetch('/api/books.php') .then(response => response.json()) .then(data => this.books = data) .catch(error => console.error(error)); } } }); new Vue({ el: '#app' }); ``` 以上就是使用 MVC 和 Vue.js 连接 MySQL 数据库的一个简单示例。实际开发中,还需要考虑安全性、性能、代码复用等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值