1. Go to this page and download the library: Download mathsgod/r-db library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mathsgod / r-db example snippets
use function R\DB\Q;
print_r(Q("User")->get()); // select * from User
class User{
}
print_r(Q(User::class)->get()); // select * from User
print_r(Q("User")->fields(["user_id","username"])->filter(["type"=>1])->get());
// select user_id,username from User where type=1
print_r(Q("User")->limit(10)->offset(0)->get()); // select * from User limit 10 offset 0
print_r(Q("User")->order("user_id desc")->get()); // select * from User order by user_id desc
use R\DB\Schema;
use R\DB\Stream;
Stream::Register(Schema::Create(), "db");
echo file_get_contents("db://User"); //List all users, User is the table name
// User can also be a class name, it will auto convert to table name
echo file_get_contents("db://User/1"); //List user with primary key 1
//List all user with fields first_name and last_name
echo file_get_contents("db://User?fields[]=first_name&fields[]=last_name");
//List user with primary key 1 and field username
echo file_get_contents("db://User/1?fields[]=user_id&fields[]=username");
$query=http_build_query([
"filters"=>[
"status"=>[
"eq"=>1
]
]
]);
echo file_get_contents("db://User?$query"); //List all user with status=1
echo file_get_contents("db://User?limit=10&offset=0"); //List first 10 users
file_exists("db://User"); //return true if table User exists
rename("db://User","db://User2"); //rename table User to User2
unlink("db://User2"); //drop table User2
class User implements SchemaAwareInterface{
public static function GetSchema(){
return $schema1;
}
}