Start
Using Medoo is very simple!
Medoo1.2 does not support PHP5.4 or below. If you are using it before 1.2 Version, please select the menu Chinese Document (<1.2)
Requirements
1, PHP>=5.4, must support PDO
2, support MySQL, MSSQL, SQLite and other databases.
3. Make sure that the xxx data extension of php_pdo_xxx (xxx = database type) has been correctly installed and enabled.
4. You need to know some SQL knowledge.
PHP PDO extension list
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 installation
medoo requires PHP to support PDO extensions, please install the relevant extensions Continue the following operations
// 打开php.ini找到你想要的相应扩展,去掉前面的;号即可 // 将 ;extension=php_pdo_mysql.dll // 修改成 extension=php_pdo_mysql.dll // 保存,重启你的PHP或者服务器 //如果PDO安装成功,你可以通过phpinfo()查看到它.
If you install through the terminal (linux) command line, the system will automatically install and configure the corresponding extension
$ sudo apt-get install php5-mysql
Use PHP Composer to install
If you install it through the dependency extension that comes with PHP, you can use the following command, or you can modify it according to your own needs.
$ composer require catfan/Medoo
Source file installation
This is the simplest method, download the medoo source file, put it in your PHP development directory, and load it
require 'medoo.php';
Medoo configuration
Here provides three database connection demonstrations.
// 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' => '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" => "foo@bar.com" ]);
Customized DSN link
Missing You can also use custom DSN connections for databases that Medoo does not support, especially for new databases where the DSN parameters are special, or if you want to add more DSN parameter values to the connection.
Connection format.
{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
Connect to SQLite
If you want to use Medoo to connect to your MSSQL database, you need to install the relevant extension: Windows installation pdo_sqlsrv , Linux/UNIX install pdo_dblib. The pdo_mssql extension has been abandoned by PHP and is not recommended.
$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' ]);
Now Medoo can use sqlsrv to drive MSSQL. For details, see Microsoft official documentation 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' ]);
Connect to SQLite
$database = new medoo([ 'database_type' => 'sqlite', 'database_file' => 'my/database/path/database.db' ]); $database->insert("account", [ "user_name" => "foo", "email" => "foo@bar.com" ]);
The above is the detailed content of PHP database framework Medoo1.6 installation tutorial. For more information, please follow other related articles on the PHP Chinese website!