PHP封装Medoo类

参考:https://medoo.in/doc

Composer安装:

composer require catfan/medoo
composer update

封装Mysql类:

require __DIR__.'/vendor/autoload.php';

use Medoo\Medoo;

class Mysql {

    private $database;
    private $server = '127.0.0.1';
    private $username = 'root';
    private $password = '123456';
    private $port = 3306;

    public function __construct($database_name){
        $this->database = new Medoo([
            'database_type' => 'mysql',
            'database_name' => $database_name,
            'server' => $this->server,
            'username' => $this->username,
            'password' => $this->password,
            'port' => $this->port
        ]);
    }

    /**
     * @param string $table
     * @param array $data
     * @return bool|PDOStatement
     */
    public function insert($table, $data){
        return $this->database->insert($table,$data);
    }

    /**
     * @param string $query
     * @return array
     */
    public function query($query){
        return $this->database->query($query)->fetchAll();
    }

    /**
     * @param string $table
     * @param array $data
     * @param array $where
     * @return bool|PDOStatement
     */
    public function update($table, $data, $where){
        return $this->database->update($table, $data, $where);
    }

    /**
     * @param string $table
     * @param array $where
     */
    public function delete($table, $where){
        return $this->database->delete($table, $where);
    }

}

调用示例:

    //写到数据库
    $mydata = array(
        'name' => '唐小佳',
        'age'     => 17,
        'create_time' => date('Y-m-d H:i:s',time()),
    );
    $mysql = new Mysql('database_name');
    $mysql->insert('table',$mydata);

 

 

 

你可能感兴趣的:(mysql,php)