PHP数据库框架Medoo1.6安装教程

开始

使用Medoo是非常简单的事!

Medoo1.2开始不支持PHP5.4或以下,若你使用的是1.2以前的版本,请选择菜单 中文文档(<1.2)

要求

1、PHP>=5.4, 必须支持PDO

2、支持 MySQL, MSSQL, SQLite 等数据库.

3、确保php_pdo_xxx (xxx = 数据库类型) 的xxx数据扩展已经正确安装并启用.

4、需要懂一些SQL知识.

PHP PDO扩展列表

MySQL, MariaDB -> php_pdo_mysql

MSSQL (Windows) -> php_pdo_sqlsrv

MSSQL (Liunx/UNIX) -> php_pdo_dblib / php_pdo_sqlsrv

Oracle -> php_pdo_oci

Oracle version 8 -> php_pdo_oci8

SQLite -> php_pdo_sqlite

PostgreSQL -> php_pdo_pgsql

Sybase -> php_pdo_dblib

PHP PDO安装

medoo需要PHP支持PDO扩展,请在安装相关扩展后继续以下操作

1

2

3

4

5

6

7

// 打开php.ini找到你想要的相应扩展,去掉前面的;号即可

// 将

;extension=php_pdo_mysql.dll

// 修改成

extension=php_pdo_mysql.dll

// 保存,重启你的PHP或者服务器

//如果PDO安装成功,你可以通过phpinfo()查看到它.

如果你通过终端(linux)命令行安装,系统会自动安装配置相应扩展

1

$ sudo apt-get install php5-mysql

使用 PHP Composer 安装

如果你通过php自带的依赖扩展安装它,可以使用下面的命令,或者你根据自己的需要修改即可。

1

$ composer require catfan/Medoo

源文件安装

这是最简单的方法,下载medoo源文件,放到你的PHP开发目录里,载入即可

1

require  ‘medoo.php‘;

Medoo配置

这儿提供三种数据库的连接演示.

1

2

3

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

// If you installed via composer, just use this code to requrie autoloader on the top of your projects.

require ‘vendor/autoload.php‘;

  

// Using Medoo namespace

use Medoo\Medoo;

  

$database = new Medoo([

    // required

    ‘database_type‘ => ‘mysq<a target="_blank" href="https://www.ancii.com/link/v1/Ywb4wRPJs-amT19sCRwTt5xmRKP1YiC0vOQ0YZ2dG8Q/" rel="nofollow" title="l">l</a>‘,

    ‘database_name‘ => ‘nam<a target="_blank" href="https://www.ancii.com/link/v1/Q3Ezad7JMmxUPijWQa7hIfNgQ1IjKzI-xFAvxNkVSdM/" rel="nofollow" title="e">e</a>‘,

    ‘server‘ => ‘localhost‘,

    ‘username‘ => ‘your_username‘,

    ‘password‘ => ‘your_password‘,

  

    // [optional]

    ‘charset‘ => ‘utf8‘,

    ‘port‘ => 3306,

  

    // [optional] Table prefix

    ‘prefix‘ => ‘PREFIX_‘,

  

    // [optional] Enable logging (Logging is disabled by default for better performance)

    ‘logging‘ => true,

  

    // [optional] MySQL socket (shouldn‘t be used with server and port)

    ‘socket‘ => ‘/tmp/mysql.sock‘,

  

    // [optional] driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php

    ‘option‘ => [

        PDO::ATTR_CA<a target="_blank" href="https://www.ancii.com/link/v1/muz_g0akXTkOUHs5IhJpEfiSzd7y0FvZOkTZg2uOHMY/" rel="nofollow" title="S">S</a>E => PDO::CASE_NATURAL

    ],

  

    // [optional] Medoo will execute those commands after connected to the database for initialization

    ‘command‘ => [

        ‘SET SQL_MODE=ANSI_QUOTES‘

    ]

]);

  

$database->insert("account", [

    "user_name" => "foo",

    "email" => ""

]);

定制DSN链接

缺省情况下Medoo不支持的数据库您还可以使用自定义DSN连接,特别是一些新数据库,DSN参数比较特殊,或者如果要为连接添加更多的DSN参数值。

连接格式.

1

{d<a target="_blank" href="https://www.ancii.com/link/v1/LfJkkflfKAGaNtVt5m3tw_iSzd7y0FvZOkTZg2uOHMY/" rel="nofollow" title="r">r</a>iver}:{key}={value};{key}={value}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

$database = new Medoo([

    // Started using customized DSN connection

    ‘dsn‘ => [

        // The PDO driver name for DSN driver parameter

        ‘driver‘ => ‘mydb‘,

        // The parameters with key and value for DSN

        ‘server‘ => ‘12.23.34.45‘,

        ‘port‘ => ‘8886‘

    ],

    // [optional] Medoo will have different handle method according to different database type

    ‘database_type‘ => ‘mysql‘,

  

    ‘username‘ => ‘your_u<a target="_blank" href="https://www.ancii.com/link/v1/MjFw1bGf3lqlRNvkx5-jhbLXegZ64S9Zd4jq28fkvrw/" rel="nofollow" title="s">s</a>ername‘,

    ‘password‘ => ‘your_password‘

]);

  

// The final DSN connection string will be generated like this

mydb:server=12.23.34.45;port=8886

连接 SQLite

如果你要使用Medoo连接你的MSSQL数据库,你需要安装相关扩展:Windows安装pdo_sqlsrv、Linux/UNIX安装pdo_dblib. pdo_mssql 扩展已被PHP废弃,不建议使用.

1

2

3

4

5

6

7

8

9

10

11

12

13

$database = new Medoo([

    ‘database_type‘ => ‘mysql‘,

    ‘database_name‘ => ‘name‘,

    ‘server‘ => ‘localhost‘,

    ‘username‘ => ‘your_username‘,

    ‘password‘ => ‘your_password‘,

  

    // [optional] The application name

    ‘appname‘ => ‘test‘,

  

    // [optional] If you w<a target="_blank" href="https://www.ancii.com/link/v1/2_XwJXcNGVQn4oKuaXHzJHELXxeK_M-G983jtWae6MM/" rel="nofollow" title="a">a</a>nt to force Medoo to use dblib driver for connecting MSSQL database

    ‘driver‘ => ‘dblib‘

]);

现在Medoo可以使用sqlsrv来驱动MSSQL,详见微软官方文档 https://docs.microsoft.com/en-us/sql/connect/php/connection-options?view=sql-server-2017.

1

2

3

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

$database = new Medoo([

    ‘database_type‘ => ‘mysql‘,

    ‘database_name‘ => ‘name‘,

    ‘server‘ => ‘localhost‘,

    ‘username‘ => ‘your_username‘,

    ‘password‘ => ‘your_password‘,

  

    // [optional] MSSQL connection options

    ‘application_intent‘ => ‘ReadOnly‘,

    ‘attach_db_file_name‘ => ‘./database.sql‘,

    ‘authentication‘ => ‘SqlPassword‘,

    ‘column_encryption‘ => ‘Enabled‘,

    ‘connection_pooling‘ => 1,

    ‘encrypt‘ => 1,

    ‘failover_partner‘ => ‘MultiSubnetFailover‘,

    ‘key_store_authentication‘ => ‘KeyVaultPassword‘,

    ‘key_store_principal_id‘ => ‘AzureName‘,

    ‘key_stor<a target="_blank" href="https://www.ancii.com/link/v1/JvJ28PMBIWhSYFIVdn9co0FrSV9Ni9CF2Q7eZpnceT4/" rel="nofollow" title="e_secre">e_secre</a>t‘ => ‘AzurePass‘,

    ‘login_timeout‘ => ‘20‘,

    ‘multiple_active_result_sets‘ => 1,

    ‘multi_subnet_failover‘ => ‘Yes‘,

    ‘scrollable‘ => ‘buffered‘,

    ‘trace_file‘ => ‘./path‘,

    ‘trace_on‘ => 1,

    ‘transaction_isolation‘ => PDO::SQLSRV_TXN_SNAPSHOT,

    ‘transparent_network_ip_resolution‘ => ‘Enabled‘,

    ‘trust_server_certificate‘ => 1,

    ‘wsid‘ => ‘Computer1‘

]);

连接 SQLite

1

2

3

4

5

6

7

8

9

$database = new Medoo([

    ‘database_type‘ => ‘sqlite‘,

    ‘database_file‘ => ‘my/database/path/database.db‘

]);

  

$database->insert("acco<a target="_blank" href="https://www.ancii.com/link/v1/McCMJqSyyMyQ8F6kNI7fw6b0M-GNlWIWxsAiw4OdtJM/" rel="nofollow" title="u">u</a>nt", [

    "user_name" => "foo",

    "email" => ""

]);

以上就是PHP数据库框架Medoo1.6安装教程的详细内容,

相关推荐