zephir开发的扩展“wudimei框架”之数据库使用方法

很喜欢laravel,以前写了一个wudimei php (https://github.com/wudimeicom/wudimeiphp),就是学习laravel的方法名、类名、连写。

现在用zephir写成扩展。把wudimei 框架编译成wudimei.so,复制到php7.2.3的安装目录。然后放在php.ini中。extension=wudimei.so

我使用的是ubuntu kylin 17.10 x86_64的操作系统。

使用该框架时,不需要require_once wudimei框架的php文件,因为它本身是扩展,没有相应的php文件。

  1 <?php
  2 ini_set("display_errors",1);
  3 error_reporting(E_ALL);
  4 
  5 use Wudimei\DB;
  6 
  7 $cfg = [
  8 'driver'=>'mysql',
  9 'host'=>'127.0.0.1',
 10 'database'=>'wudimei_mvc',
 11 'username'=>'root',
 12 'password'=>'123456',
 13 'charset'=>'utf8', 
 14 'table_prefix'=>'w_'
 15 ];
 16 
 17 DB::addConnection($cfg);
 18 
 19 function display_result($data,$obj =null ){
 20     echo '<h3>result</h3>';
 21     echo '<pre>'; print_r( $data ); echo '</pre>';
 22     
 23     if( $obj == null ){
 24         echo '<h3>sql</h3>';
 25         echo htmlspecialchars( DB::getLastSql() );
 26         echo '<h3>Input Parameters</h3>';
 27         print_r( DB::getLastParams()  );
 28         echo '<h3>error</h3>';
 29         print_r( DB::error() );
 30     }
 31     else{
 32         echo '<h3>sql</h3>';
 33         echo htmlspecialchars($obj->getLastSql());
 34         echo '<h3>Input Parameters</h3>';
 35         print_r( $obj->getLastParams()  );
 36         echo '<h3>error</h3>';
 37         print_r( $obj->error() );
 38     }
 39     echo '<hr />';
 40 }
 41 /*
 42 $ar = DB::table("blog")->where('id',9)->delete();
 43 display_result( $ar );
 44 
 45 
 46 
 47 $ar = DB::table("blog")->where('id',6)->update(["title"=>"wudimei6.12","content"=>"wudimei6.1",
 48     //"created_at"=>date("Y-m-d H:i:s")
 49 ]);
 50 display_result( $ar );
 51  */
 52  //只查看selct型sql语句和参数,不执行
 53  $obj= DB::selectRaw('id*? as id2,title',[10])->from('blog')->where("id",1);
 54  echo $obj->getSql();
 55  $params = $obj->getInputParameters();
 56  print_r( $params );
 57  
 58  //$obj = DB::selectRaw('id*? as id2,title',[10])->from('blog')->where("id",1)->orWhere('id',2);
 59 //$obj = DB::select('id,title')->from('blog')->where("id",1)->orWhere('id',2);
 60 //$obj = DB::select('s.id,s.name,sg.group_name')->from('settings','s')->addFrom('setting_groups','sg')->where("id",1)->whereRaw('s.setting_group_id=sg.id');
 61 //$obj = DB::selectRaw('id*? as id2,title',[10])->from('blog')->whereRaw("id=?",[1])->orderBy('id','desc')->limit(0,1);
 62 //$obj = DB::selectRaw('id*? as id2,title',[10])->from('blog')->whereRaw("id=?",[1])->orderBy('id','desc')->limit(0,1);
 63 //$obj = DB::select('id,title')->from('blog')->groupBy("title");
 64 //$obj = DB::select('id,title,count(id) as cnt')->from('blog')->groupBy("title")->having('id','>', 0);
 65 //$obj = DB::select('id,title,count(id) as cnt')->from('blog')->groupBy("title")->havingRaw('id > ?', [1] )->orHavingRaw('title <> ?', ['test'] );
 66 /* $obj = DB::select('id,title,count(id) as cnt')->from('blog')
 67         ->groupBy("title")->having('id','>', 0 )->orHaving('title', '<>','test'); */
 68 /*
 69 $obj = DB::select('id,title,count(id) as cnt')->from('blog')
 70         ->groupBy("title")->having('id','>', 0 )->having('title', '<>','test');*/
 71 /*
 72   $obj = DB::table('blog')->where('id','>',0);
 73   echo $obj->count("id");   echo " , ";
 74   echo $obj->max("id");   echo " , ";
 75   echo $obj->min("id");   echo " , ";
 76   echo $obj->sum("id");   echo " , ";
 77   echo $obj->avg("id");   */
 78   $obj = DB::table('blog')->where('id','>',0);
 79   $obj2 =   $obj;
 80   //分类,第一个参数是分页大小,第二个是页码,从1页开始。
 81   $dt = $obj->paginate(5 );
 82   display_result( $dt );
 83   display_result( $obj2->get(), $obj2 );
 84   /*
 85   
 86   
 87  */
 88   $obj = DB::selectRaw('id*? as id2,title',[10])->from('blog')->where("id",1)->orWhere('id',2);
 89   display_result( $obj->get() );
 90   display_result( $obj->where("id",'<',2)->first() );
 91   print_r( DB::table('blog')->first() );
 92  // $dt = $obj->get();
 93 /*
 94 $obj = DB::select('s.id,s.name,sg.group_name')
 95 ->from('settings','s')
 96 ->leftJoin('setting_groups','sg')
 97 //->on('s.setting_group_id','=','sg.id')
 98 ->onRaw('s.setting_group_id = sg.id')
 99 ->where("s.id",1);
100 */
101 //where raw
102 
103 
104 
105  
106 
107 /*
108 $id = DB::table("blog")->insert(["title"=>"wudimei6","content"=>"wudimei6","created_at"=>date("Y-m-d H:i:s")]);
109 echo $id;
110 print_r( DB::error() );
111 */
112 /*
113 $blog = new \stdClass();
114 $blog->title="wudimei8";
115 $blog->content="wudimei8";
116 $blog->created_at= date("Y-m-d H:i:s");
117 $id = DB::table("blog")->insert($blog);
118 echo $id;
119 print_r( DB::error() );
120 */

输出

SELECT id*? as id2,title FROM w_blog WHERE id=?Array ( [0] => 10 [1] => 1 )

result

stdClass Object
(
    [first] => 1
    [last] => 2
    [page] => 2
    [page_size] => 5
    [total] => 8
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 6
                    [title] => wudimei6.12
                    [content] => wudimei6.1
                    [created_at] => 2018-06-04 06:34:48
                )

            [1] => stdClass Object
                (
                    [id] => 7
                    [title] => wudimei7
                    [content] => wudimei7
                    [created_at] => 2018-06-04 04:33:13
                )

            [2] => stdClass Object
                (
                    [id] => 8
                    [title] => wudimei8
                    [content] => wudimei8
                    [created_at] => 2018-06-04 04:33:43
                )

        )

)

sql

SELECT count(*) as wudimei_num_1 FROM w_blog WHERE id >? ;;; SELECT * FROM w_blog WHERE id >? LIMIT 5,5 ;

Input Parameters

Array ( [sqlCntParams] => Array ( [0] => 0 ) [sqlQueryParams] => Array ( [0] => 0 ) )

error

Array ( [cntError] => Array ( [0] => 00000 [1] => [2] => ) [queryError] => Array ( [0] => 00000 [1] => [2] => ) )


result

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [title] => wudimei
            [content] => wudimei
            [created_at] => 2018-06-04 04:15:37
        )

    [1] => stdClass Object
        (
            [id] => 2
            [title] => wudimei2
            [content] => wudimei2
            [created_at] => 2018-06-04 04:15:58
        )

    [2] => stdClass Object
        (
            [id] => 3
            [title] => wudimei3
            [content] => wudimei3
            [created_at] => 2018-06-04 04:16:14
        )

    [3] => stdClass Object
        (
            [id] => 4
            [title] => wudimei4
            [content] => wudimei4
            [created_at] => 2018-06-04 04:16:35
        )

    [4] => stdClass Object
        (
            [id] => 5
            [title] => wudimei5
            [content] => wudimei5
            [created_at] => 2018-06-04 04:16:42
        )

    [5] => stdClass Object
        (
            [id] => 6
            [title] => wudimei6.12
            [content] => wudimei6.1
            [created_at] => 2018-06-04 06:34:48
        )

    [6] => stdClass Object
        (
            [id] => 7
            [title] => wudimei7
            [content] => wudimei7
            [created_at] => 2018-06-04 04:33:13
        )

    [7] => stdClass Object
        (
            [id] => 8
            [title] => wudimei8
            [content] => wudimei8
            [created_at] => 2018-06-04 04:33:43
        )

)

sql

SELECT * FROM w_blog WHERE id >?

Input Parameters

Array ( [0] => 0 )

error

Array ( [0] => 00000 [1] => [2] => )


result

Array
(
    [0] => stdClass Object
        (
            [id2] => 10
            [title] => wudimei
        )

    [1] => stdClass Object
        (
            [id2] => 20
            [title] => wudimei2
        )

)

sql

SELECT id*? as id2,title FROM w_blog WHERE id=? OR id=?

Input Parameters

Array ( [0] => 10 [1] => 1 [2] => 2 )

error

Array ( [0] => 00000 [1] => [2] => )


result

stdClass Object
(
    [id2] => 10
    [title] => wudimei
)

sql

SELECT id*? as id2,title FROM w_blog WHERE id=? OR id=? AND id <? LIMIT 0,1

Input Parameters

Array ( [0] => 10 [1] => 1 [2] => 2 [3] => 2 )

error

Array ( [0] => 00000 [1] => [2] => )


stdClass Object ( [id] => 1 [title] => wudimei [content] => wudimei [created_at] => 2018-06-04 04:15:37 )

猜你喜欢

转载自www.cnblogs.com/wudimei/p/9134148.html
今日推荐