php [Medoo sql wrapper] Medoo mysql,sqlite,.. wrapper #php
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [Medoo sql wrapper] Medoo mysql,sqlite,.. wrapper #php相关的知识,希望对你有一定的参考价值。
<?php
/*
https://github.com/catfan/Medoo
composer require catfan/Medoo
*/
use Medoo\Medoo;
/**
Initialize
*/
$database = new Medoo([
// required
'database_type' => 'mysql',
'database_name' => 'name',
'server' => 'localhost',
'username' => 'your_username',
'password' => 'your_password',
// [optional]
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'port' => 3306,
// [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'
]
]);
//PDO Object Initialization
$pdo = new PDO('mysql:dbname=test;host=127.0.0.1', 'user', 'password');
$database = new Medoo([
// Initialized and connected PDO object
'pdo' => $pdo,
// [optional] Medoo will have different handle method according to different database type
'database_type' => 'mysql'
]);
//Get information about database connection
print_r($database->info());
//## For SQLite
$database = new Medoo([
'database_type' => 'sqlite',
'database_file' => 'my/database/path/database.db'
]);
// Memory database
$database = new Medoo([
'database_type' => 'sqlite',
'database_file' => ':memory:'
]);
/**------------------------------------------------------------------------
query
*/
$database->query("CREATE TABLE table (
c1 INT STORAGE DISK,
c2 INT STORAGE MEMORY
) ENGINE NDB;");
//check error
var_dump( $database->error() );
/**------------------------------------------------------------------------
select
*/
//query
$data = $database->query("SELECT email FROM account")->fetchAll();
#Prepared Statement
$data = $database->query(
"SELECT * FROM <account> WHERE <user_name> = :user_name AND <age> = :age", [
":user_name" => "John Smite",
":age" => 20
]
)->fetchAll();
$datas = $database->select('account', [
'user_name',
'email'
], [
'user_id' => 50, //"user_id[>]" => 100
"AND" => [
"type" => "business",
"age[<]" => 18
]
],[
"LIMIT" => [0, 2]
]);
foreach($datas as $data)
{
echo "user_name:" . $data["user_name"] . " - email:" . $data["email"] . "<br/>";
}
#echo json_encode($datas);
//debug sql
$database->debug()->select("bccount", [
"user_name",
"email"
], [
"user_id[<]" => 20
]);
# Will output:
# SELECT "user_name","email" FROM "bccount" WHERE "user_id" < 20
//get last query performed
echo $database->last(); //INSERT INTO "account" ("user_name", "email") VALUES ('foo', 'foo@bar.com')
// Select all columns
$datas = $database->select("account", "*");
// Select a column
$datas = $database->select("account", "user_name");
//get row
$email = $database->get("account", "email", [
"user_id" => 1234
]);
$profile = $database->get("account", [
"email",
"gender",
"location"
], [
"user_id" => 1234
]);
//check exists
if ($database->has("account", [
"AND" => [
"OR" => [
"user_name" => "foo",
"email" => "foo"
],
"password" => "12345"
]
]))
{
echo "Password is correct.";
}
//Fetch randomized data
$data = $database->rand("account", [
"user_name",
"email"
], [
"user_id[>]" => 100
]);
//count
$count = $database->count("account", [
"gender" => "female"
]);
/**------------------------------------------------------------------------
insert
*/
$data = $database->insert('account', [
'user_name' => 'foo',
'email' => 'foo@bar.com',
"lang" => ["en", "fr", "jp", "cn"] // => 'a:4:{i:0;s:2:"en";i:1;s:2:"fr";i:2;s:2:"jp";i:3;s:2:"cn";}'
"lang [JSON]" => ["en", "fr", "jp", "cn"] // => '["en","fr","jp","cn"]'
//SQL Functions
"uid" => Medoo::raw("UUID()")
]);
//support multiple insertion
$database->insert('account', [[],[]]);
echo $data->rowCount();
//insert id
$database->id();
/**------------------------------------------------------------------------
update
*/
$data = $database->update("account", [
// All age plus one
"age[+]" => 1,
// All level subtract 5
"level[-]" => 5,
// All score multiplied by 2
"score[*]" => 2,
// Array value
"lang" => ["en", "fr", "jp", "cn"],
// Array value encoded as JSON
"lang [JSON]" => ["en", "fr", "jp", "cn"],
// Boolean value
"is_locked" => true,
// assign # for using SQL functions
"#uid" => "UUID()"
],[
"user_id[<]" => 1000
]);
echo $data->rowCount();
/**------------------------------------------------------------------------
delete
*/
$t=$database->delete("account", ["user_id[<]" => 1000]);
echo $t->rowCount();
以上是关于php [Medoo sql wrapper] Medoo mysql,sqlite,.. wrapper #php的主要内容,如果未能解决你的问题,请参考以下文章
非常易用的PHP数据库框架Medoo 2.1:初识Medoo
非常易用的PHP数据库框架Medoo 2.1:使用 Medoo 连接MYSQL数据库