微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Medoo入门:安装和配置-Medoo使用指南

什么是Medoo? The Lightest PHP database framework to accelerate development! 加速开发的最轻量的PHP数据库框架! Medoo强大而完善的API不仅完全可以满足你最常用最基本的需求,而且使用起来也非常简单。即使要进行二次开发,也丝毫不成问题。首先翻译的是 Medoo的入门篇,先讲讲Medoo的安装和配置。

    Medoo是什么呢?看看官方是怎么写的: 

 The Lightest PHP database framework to accelerate development! 

//译:  加速开发的最轻量的PHP数据库框架! 

    Medoo的口号还是喊得很响的,实际上是否真的好用呢?经过一段时间的使用,我发现Medoo真的就像官方介绍的那样,可以极大地加快你的PHP项目的开发。在进行Web开发的时候,很大一部分企业或个人都会选择使用PHP语言,而Medoo则可以起到锦上添花的作用。缩短你的开发周期。Medoo强大而完善的API不仅完全可以满足你最常用最基本的需求,而且使用起来也非常简单。即使要进行二次开发,也丝毫不成问题。虽然如此,但是网上的中文文档还是不够完善。因此,我决定自己对Medoo进行翻译和整理。也许不能够非常准确,但是还是想在不遗漏官方传达的意思的基础上,尽量做到通俗易懂。那么,就从入门篇开始吧。 

    入门

    使用Medoo就是这么简单! 

    需求

    必须 PHP 5.1以上,非常、特别、极其、强烈推荐:使用PHP 5.4以上版本并开启PDO支持。 你可以安装MysqL,MSsqlsqlite等数据库,或者更多其它数据库。 确保 PHP_pdo_xxx(XXX = 你想要使用的数据库名)扩展在PHP.ini中被安装并启用。 我想你也许需要一点点的sql基本知识。(作为开发人员,我想这不成问题 ^^!) 

    温馨提示

    关于PHP 5.4+中,你可以使用 [] 作为数组的缩写语法。所有Medoo示例代码都使用 [] 来替代 array() 。 

?
1
2
3
4
// PHP 5.1 中
var  data =  array ( "foo" "bar" );
// PHP 5.4+ 中
data = [ ];


    安装

    只要下载 medoo.PHP 并把它放到正确的目录,就一切OK了! 

    配置

    有两种方法来配置medoo和启动一个数据库连接。 

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 1. 独立配置的方法
require   'medoo.PHP' ;
$database  new  medoo([
     // required
     'database_type'  =>  'MysqL' PHP plain" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
'database_name'  'name' PHP plain" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
'server'  'localhost' PHP plain" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
'username'  'your_username' PHP plain" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
'password'  'your_password' PHP plain" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
//可选
'port'  => 3306,
'charset'  'utf8' PHP plain" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
// DB连接驱动选项,了解更多 http://www.PHP.net/manual/en/pdo.setattribute.PHP
'option'  => [
         PDO::ATTR_CASE => PDO::CASE_NATURAL
     ]]);
$database ->insert( "account" PHP plain" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, [
"user_name"  PHP plain" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"email"  "foo@bar.com" ]);
// 2. 直接打开 medoo.PHP 并在文件的顶部编辑一些配置信息,
// 然后你之后就无需再次进行配置而直接使用它。
// MysqL, MSsql, Postgresql, Sybase 的类型名如下:
// MysqL -> MysqL// MSsql -> mssql
// Postgresql -> pgsql
// Sybase -> sybase
class  medoo{
     protected  $database_type 
// DB的类型名
$server  ;
$username  ;
$password  ;
// 可选
$port  = 3306;
$charset  ;
....
}
// OK, 现在一切就绪了!
require_once  ;
medoo( 'my_database' );
PHP variable" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, [
PHP spaces" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
]
);

    对于sqlite

    对于MSsql 如果你想在PHP平台下通过Medoo使用MSsql,你需要在Windows环境下安装pdo_sqlsrv PHP扩展,或者在Liunx/UNIX环境下安装pdo_dblib PHP扩展。pdo_mssql扩展已经被废除并且很快将会从PHP移除。 

20
// 1. 尽管编辑 medoo.PHP 文件里的DB类型名
medoo{
'sqlite' ;
// For sqlite [optional]
$database_file  'my/database/path/database.db' ;
....
}
 
PHP comments" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, 现在一切就绪了!
;
);
// 2. 或者独立配置
medoo([
PHP spaces" style="white-space:Nowrap; padding:0px!important; margin:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
'database_file'  ]
);
]
 

    PHP中的PDO驱动程序的安装

Medoo需要用到PHP的PDO扩展。如果你以前没有安装,请执行以下步骤。 

?
9
//  打开PHP安装目录下的PHP.ini文件,找到以下这一行并把行首的分号  ';' 去掉。其它类型数据库请分别对应移除。
移除  ';'  之前
;extension=PHP_pdo_MysqL.dll
 
之后
extension=PHP_pdo_MysqL.dll
 
然后保存并重启Apache服务器
如果安装成功的话,你就可以通过 PHPinfo() 看到 PDO扩展相关信息。

    

    OK,现在终于轻轻松松就把DB操作的框架搞定了。是不是非常简单呢。 ^^!


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐