PHP code example of myqee / database
1. Go to this page and download the library: Download myqee/database 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/ */
myqee / database example snippets
$db = DB::instance();
$name = "test'abc"; // 注意这个字符串带单引号'的
$rs = $db->from('mytable')->orderBy('id', 'DESC')->where('id', 10, '>')->limit(100)->where('name', $name)->get();
foreach($rs as $item)
{
print_r($item);
}
use \MyQEE\Database\DB;
$db1 = DB::instance();
$db2 = DB::instance();
$db3 = DB::instance('test');
$db4 = DB::instance('test');
$db5 = new DB();
var_dump($db1 === $db2); //bool(true)
var_dump($db3 === $db4); //bool(true)
var_dump($db1 === $db3); //bool(false)
var_dump($db1 === $db5); //bool(false)
use \MyQEE\Database\DB;
$db = DB::instance();
$arr = $db->from('mytable')->where('id', 1)->get()->asArray();
$db->where('id',1);
$db->from('mytable');
$rs = $db->get();
echo $rs->count(); // 行数
foreach ($rs as $item)
{
...
}
$db->from('mytable')->where('id', 1)->get();
echo $db->lastQuery(); //SELECT * FROM `mytable` where `id` = 1
$db = DB::instance();
$data = [
'title' => 'test',
'count' => 1,
];
$where = [
'id' => 1,
];
$rs = $db->update('test',$data,$where);
echo $rs; //影响的行数,若返回的是0,则表示没有更新到数据
echo $db->lastQuery(); //UPDATE `test` SET `title` = 'test', `count` = 1 WHERE `id` = 1
// 同上
$db->where($where)->update('test',$data);
// 同上
$db->where($where)->set($data)->table('test')->update();
$db = DB::instance();
$data = [
'title' => 'test',
'count' => 1,
];
// 若操作失败返回false,否则返回一个数组
$rs = $db->insert('test',$data);
print_r($rs) //返回的是一个数组,例如:array(10,1); 其中10表示自增ID号,1表示作用行数
$db = DB::instance();
$rs = $db->delete('test' , array('id'=>1) );
echo $rs; // 作用行数,0表示没有删除数据,1表示删除1行,2表示删除了2行,以此类推
$db = DB::instance();
// 返回 `class_id` = 1 条件下mytable表行数
echo $db->where('class_id' , 1)->countRecords('mytable');
//SELECT COUNT(1) AS `totalRowCount` FROM `mytable` WHERE `class_id` = 1
echo $db->lastQuery();
$db = DB::instance();
$arr = $db->query('SELECT * FROM `'.$db->table_perfix().'mytable` where `id` = 1')->asArray();
// 效果同上
$arr = $db->from('mytable')->where('id', 1)->get()->asArray();
$sql = $db->from('mytable')->where('id', 1)->compile();
echo $sql; //SELECT * FROM `mytable` where `id` = '1'
$sql = $db->value('t',1)->table('mytable')->compile('update');
echo $sql; //UPDATE `mytable` SET `t` = 1
$db = DB::instance();
$arr = $db->query('SELECT * FROM `mytable` where `id` = 1')->asArray();
print_r($arr);
// 支持直接返回一个stdClass对象
$arr = $db->query('SELECT * FROM `mytable` where `id` = 1',true)->asArray();
var_dump( $arr[0] instanceof strClass ); //bool(true)
// 支持直接返回一个用户自定义对象
$arr = $db->query('SELECT * FROM `mytable` where `id` = 1','MyClass')->asArray();
var_dump( $arr[0] instanceof MyClass ); //bool(true)