Rumah > pembangunan bahagian belakang > tutorial php > PHP数据库框架Medoo1.6安装教程

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

Guanhui
Lepaskan: 2023-04-08 15:46:01
asal
2454 orang telah melayarinya

开始

使用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()查看到它.
Salin selepas log masuk

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

$ sudo apt-get install php5-mysql
Salin selepas log masuk

使用 PHP Composer 安装

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

$ composer require catfan/Medoo
Salin selepas log masuk

源文件安装

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

require  &#39;medoo.php&#39;;
Salin selepas log masuk

Medoo配置

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

// If you installed via composer, just use this code to requrie autoloader on the top of your projects.
require &#39;vendor/autoload.php&#39;;
 
// Using Medoo namespace
use Medoo\Medoo;
 
$database = new Medoo([
    // required
    &#39;database_type&#39; => &#39;mysql&#39;,
    &#39;database_name&#39; => &#39;name&#39;,
    &#39;server&#39; => &#39;localhost&#39;,
    &#39;username&#39; => &#39;your_username&#39;,
    &#39;password&#39; => &#39;your_password&#39;,
 
    // [optional]
    &#39;charset&#39; => &#39;utf8&#39;,
    &#39;port&#39; => 3306,
 
    // [optional] Table prefix
    &#39;prefix&#39; => &#39;PREFIX_&#39;,
 
    // [optional] Enable logging (Logging is disabled by default for better performance)
    &#39;logging&#39; => true,
 
    // [optional] MySQL socket (shouldn&#39;t be used with server and port)
    &#39;socket&#39; => &#39;/tmp/mysql.sock&#39;,
 
    // [optional] driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
    &#39;option&#39; => [
        PDO::ATTR_CASE => PDO::CASE_NATURAL
    ],
 
    // [optional] Medoo will execute those commands after connected to the database for initialization
    &#39;command&#39; => [
        &#39;SET SQL_MODE=ANSI_QUOTES&#39;
    ]
]);
 
$database->insert("account", [
    "user_name" => "foo",
    "email" => "foo@bar.com"
]);
Salin selepas log masuk

定制DSN链接

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

连接格式.

{driver}:{key}={value};{key}={value}
Salin selepas log masuk
$database = new Medoo([
    // Started using customized DSN connection
    &#39;dsn&#39; => [
        // The PDO driver name for DSN driver parameter
        &#39;driver&#39; => &#39;mydb&#39;,
        // The parameters with key and value for DSN
        &#39;server&#39; => &#39;12.23.34.45&#39;,
        &#39;port&#39; => &#39;8886&#39;
    ],
    // [optional] Medoo will have different handle method according to different database type
    &#39;database_type&#39; => &#39;mysql&#39;,
 
    &#39;username&#39; => &#39;your_username&#39;,
    &#39;password&#39; => &#39;your_password&#39;
]);
 
// The final DSN connection string will be generated like this
mydb:server=12.23.34.45;port=8886
Salin selepas log masuk

连接 SQLite

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

$database = new Medoo([
    &#39;database_type&#39; => &#39;mysql&#39;,
    &#39;database_name&#39; => &#39;name&#39;,
    &#39;server&#39; => &#39;localhost&#39;,
    &#39;username&#39; => &#39;your_username&#39;,
    &#39;password&#39; => &#39;your_password&#39;,
 
    // [optional] The application name
    &#39;appname&#39; => &#39;test&#39;,
 
    // [optional] If you want to force Medoo to use dblib driver for connecting MSSQL database
    &#39;driver&#39; => &#39;dblib&#39;
]);
Salin selepas log masuk

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

$database = new Medoo([
    &#39;database_type&#39; => &#39;mysql&#39;,
    &#39;database_name&#39; => &#39;name&#39;,
    &#39;server&#39; => &#39;localhost&#39;,
    &#39;username&#39; => &#39;your_username&#39;,
    &#39;password&#39; => &#39;your_password&#39;,
 
    // [optional] MSSQL connection options
    &#39;application_intent&#39; => &#39;ReadOnly&#39;,
    &#39;attach_db_file_name&#39; => &#39;./database.sql&#39;,
    &#39;authentication&#39; => &#39;SqlPassword&#39;,
    &#39;column_encryption&#39; => &#39;Enabled&#39;,
    &#39;connection_pooling&#39; => 1,
    &#39;encrypt&#39; => 1,
    &#39;failover_partner&#39; => &#39;MultiSubnetFailover&#39;,
    &#39;key_store_authentication&#39; => &#39;KeyVaultPassword&#39;,
    &#39;key_store_principal_id&#39; => &#39;AzureName&#39;,
    &#39;key_store_secret&#39; => &#39;AzurePass&#39;,
    &#39;login_timeout&#39; => &#39;20&#39;,
    &#39;multiple_active_result_sets&#39; => 1,
    &#39;multi_subnet_failover&#39; => &#39;Yes&#39;,
    &#39;scrollable&#39; => &#39;buffered&#39;,
    &#39;trace_file&#39; => &#39;./path&#39;,
    &#39;trace_on&#39; => 1,
    &#39;transaction_isolation&#39; => PDO::SQLSRV_TXN_SNAPSHOT,
    &#39;transparent_network_ip_resolution&#39; => &#39;Enabled&#39;,
    &#39;trust_server_certificate&#39; => 1,
    &#39;wsid&#39; => &#39;Computer1&#39;
]);
Salin selepas log masuk

连接 SQLite

$database = new medoo([
    &#39;database_type&#39; => &#39;sqlite&#39;,
    &#39;database_file&#39; => &#39;my/database/path/database.db&#39;
]);
 
$database->insert("account", [
    "user_name" => "foo",
    "email" => "foo@bar.com"
]);
Salin selepas log masuk

Atas ialah kandungan terperinci PHP数据库框架Medoo1.6安装教程. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan