Medooデータベースの基本操作

17952 ワード

Medooクエリー操作
データベース照会select($table, $columns, $where)
  • table[string]テーブル名.
  • columns[string/array]クエリーするフィールド名.
  • where(optional)[array]クエリの条件.

  • 共通ファイルを作成connect.php
    
    //      php     。            
    require 'vendor/autoload.php';
    
    //      
    use Medoo\Medoo as Db;
    
    //      
    $config = [
        //   
        'database_type' => 'mysql',
        'database_name' => 'php_edu',
        'server' => 'localhost',
        'username' => 'wjh',
        'password' => '1010',
    
        // [  ]
        'charset' => 'utf8',
        'port' => 3306,
    ];
    
    //   Medoo ,  db  
    $db = new Db($config);

    クエリー・インスタンス
    
    /**
     * Medoo     
     */
    
    //1.    Medoo   
    require __DIR__ . '/connect.php';
    
    //2.     
    $table = 'user';
    $fields = ['id', 'name', 'age'];
    
    //     1   
    $where = ['status'=>1];
    
    //      50   
    $where = ['age[>]'=>50];
    
    //      50        
    $where = ['AND'=>['age[>]'=>50, 'sex'=>0]];
    
    
    $rows = $db->select($table, $fields, $where);
    
    foreach ($rows as $row){
        echo print_r($row, true) . '
    '
    ; }

    Medoo挿入操作
    テーブルにデータを挿入insert($table, $data)
  • table[string]表名
  • data[array]テーブルに挿入されたデータ
  • インスタンスの挿入
    
    
    require __DIR__ . '/connect.php';
    
    $data = [
        'name' => '  ',
        'sex'  => 1,
        'age'  => 26,
        'email'=> '[email protected]',
        'password' => sha1('123456'),
        'status' => 1,
        'create_time'=> time()
    ];
    
    //  PDO     
    $stmt = $db->insert('user', $data);
    
    echo print_r($stmt, true);
    
    
    //      
    echo print_r($stmt->errorInfo(), true);

    Medoo更新操作
    テーブルデータの変更update ($table, $data, $where)
  • table[string]表名
  • data[array]修正データ
  • where(optional)[array]WHERE条件.[オプション]
  • インスタンスの更新
    
    
    //1.    Medoo   
    require __DIR__ . '/connect.php';
    
    //   
    $table = 'user';
    
    //    
    $data = [
        'name'=>'   ',
        'sex' =>1,
    ];
    
    $data = [
        'name'=>'   ',
        //   +=1
        'age[+]' =>1,
    ];
    
    
    //    
    $where = [
        'name'=>'hongqigong'
    ];
    
    //       
    $stmt = $db->update($table, $data, $where);
    
    
    //      
    echo print_r($stmt->errorInfo(), true);

    Medoo削除操作
    テーブルのデータの削除delete ($table, $where)
  • table[string]表名
  • where[array]WHERE削除条件.

  • インスタンスの削除
    
    
    //1.    Medoo   
    require __DIR__ . '/connect.php';
    
    //   
    $table = 'user';
    
    
    //    
    $where = [
        'name'=>'yangkang'
    ];
    
    //       
    $stmt = $db->delete($table, $where);
    
    //      
    echo print_r($stmt->errorInfo(), true);

    参考資料:[1]Medooフレームワークマニュアル