数据库操作采用第三方的medoo类,按照medoo的官方文档自己可以自定义一些常用的数据库操作
<?php
namespace core\lib;
use core\lib\config;
use Medoo\Medoo;
class model extends Medoo{
public function __construct(){
$option = config::getAll('database');
parent::__construct($option);
}
public function db($table){
$rs = $this->select($table,'*');
return $rs;
}
public function find($table,$id){
$rs = $this->get($table,'*',['id' => $id]);
return $rs;
}
}
medoo配置文件,官网上有直接复制下来用即可
<?php
//数据库配置文件
return [
// required
'database_type' => 'mysql',
'database_name' => 'shop',
'server' => 'localhost',
'username' => 'root',
'password' => 'root',
// [optional]
'charset' => 'utf8',
'port' => 3306,
// [optional] Table prefix
'prefix' => 'tp_',
// [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'
]
];
如何使用
<?php
namespace app\controller;
use core\lib\model;
class indexController extends \core\phpone{
public function index(){
$db = new model;
$rs = $db->find('brand',3); //第一个参数为表名,第二个参数为要查询的id
dump($rs);
}
}