PHP資料庫框架Medoo1.6安裝教學

2020-07-16 10:05:51
開始

使用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擴充套件,請在安裝相關擴充套件後繼續以下操作

// 開啟php.ini找到你想要的相應擴充套件,去掉前面的;號即可
// 將
;extension=php_pdo_mysql.dll
// 修改成
extension=php_pdo_mysql.dll
// 儲存,重新啟動你的PHP或者伺服器
//如果PDO安裝成功,你可以通過phpinfo()檢視到它.

如果你通過終端(linux)命令列安裝,系統會自動安裝設定相應擴充套件

$ sudo apt-get install php5-mysql

使用 PHP Composer 安裝

如果你通過php自帶的依賴擴充套件安裝它,可以使用下面的命令,或者你根據自己的需要修改即可。

$ composer require catfan/Medoo

原始檔安裝

這是最簡單的方法,下載medoo原始檔,放到你的PHP開發目錄裡,載入即可

require  'medoo.php';

Medoo設定

這兒提供三種資料庫的連線演示.

// 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 MedooMedoo;
 
$database = new Medoo([
    // required
    'database_type' => 'mysql',
    'database_name' => 'name',
    '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_CASE => 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" => "[email protected]"
]);

客製化DSN連結

預設情況下Medoo不支援的資料庫您還可以使用自定義DSN連線,特別是一些新資料庫,DSN引數比較特殊,或者如果要為連線新增更多的DSN引數值。

連線格式.

{driver}:{key}={value};{key}={value}
$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_username',
    '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廢棄,不建議使用.

$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 want 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.

$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_store_secret' => '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

$database = new medoo([
    'database_type' => 'sqlite',
    'database_file' => 'my/database/path/database.db'
]);
 
$database->insert("account", [
    "user_name" => "foo",
    "email" => "[email protected]"
]);

以上就是PHP資料庫框架Medoo1.6安裝教學的詳細內容,更多請關注TW511.COM其它相關文章!