MySQL Cloud database external calling scheme such as Baidu Cloud, Sina SAE database

Source: Internet
Author: User

Using the SAE database is very good. It is not available externally. It is not very good to use it inside the SAE. So it took a little bit of time to write something for external invocation. The code is simple. Part of the server is the client.

Ideally applicable to all cloud databases. But it has to be debugged according to the situation.

<?php//sae Client Code $config= array (     ' hostname '            =>   ' http://2.freedaili.sinaapp.com/server_apimysql.php ', //  Server address      ' database '           =>    ' App_freedaili ',          //  database name       ' password '           =>   ' 756663992 ' ,          //  Data key (set by server));require_once  ' Apimysql.class.php '; $db  = new apimysql ($config);//$data = $db->version ();p rint_r ($data); $data = $db->getone (' Select * from test_api order by id desc ');p rint_r ($data);// $data = $db->getall (' Select * from test_api order by id desc ');p Rint_r ($ data);//$data = $db->inserT ("Insert into test_api (name)  values (' Test ')");p Rint_r ($data);//$data = $db->update ("Update  test_api set name= ' test1 '  where id=1 ");p Rint_r ($data);//$data = $db->delete (" delete  from test_api where where id=1 ");p Rint_r ($data);//$data = $db->query (' create  table if not exists  ' Test_api '   (' id '  int (TEN)  not null auto_ INCREMENT, ' name '  varchar (255)  NOT NULL,PRIMARY KEY  (' id '))  ENGINE=MyISAM   DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; '); Print_r ($data);? >
apimysql.class.php<?php// +----------------------------------------------------------------------//  | Copyright  (c)  2115 all rights reserved.// +------------------------ ----------------------------------------------// | author: xiaogg <[email  protected]>// +----------------------------------------------------------------------class  apimysql{    //  database Connection parameter configuration     protected  $resultdata;     protected  $error;    protected  $config       = array (         ' hostname '            =>   ' 127.0.0.1 ', //  server address           ' database '           =>   ',           //  database name          ' password '            =>   ',           //  Data key     );     public function __ Construct ($config = ") {if (!empty ($config))  {$this->config   =   array_ Merge ($this->config, $config);}}     public function query ($sql  ,  $type  =  ', $querytype = ') {          $data =array (); $data [' SQL ']= $sql; $data [' type ']= $type; $data [' QueryType ']= $querytype;         $result = $this->get_result ($data); Return !empty ($result [' data '])? $result [' Data ']: $result [' msg '];    }     public function version () {return  $this->query (' Version ', ' Version ');      public function getone ($sql) {return  $this->query ($sql, ' GetOne ');}     public function getall ($sql) {return  $this->query ($sql, ' getall ');}     public function insert ($sql) {return  $this->query ($sql, ' query ', ' Insert ');}     public function update ($sql) {return  $this->query ($sql, ' query ', ' Update ');}     public function delete ($sql) {return  $this->query ($sql, ' query ', ' Delete ');}     public function num_rows ($sql) {return  $this->query ($sql, ' num_rows ');}         public function affected_rows () {Return mysql_ Affected_rows ($this->connid);}     public function insert_id ()  {return  $this->resultdata;}     function fetch_row ($query) {Return is_array ($this->resultdata)? count ($this->resultdata): $this->resultdata;}     function error () {return  $this->error;}     private function get_result ($data) {         if (Empty ($data [' type ']) $data [' type ']= ' query ';         if ( Empty ($data [' db_name ']) $data [' db_name ']= $this->config[' database '];                  $postdata = $this->formatdata ($data);          $result = $this->curlpost ($this->config[' hostname '), $postdata);          $result =json_decode ($result, true); $this->resultdata=!empty ($ result[' data ')? $result [' Data ']: '; $this->error=!empty ($result [' msg '])? $result [' msg ']: '; return $ Result;    }    private function curlpost ($url,  $params = ")  {         $opts  = array (curlopt_timeout => 5,curlopt_ Returntransfer=> 1,curlopt_ssl_verifypeer=> false,curlopt_ssl_verifyhost=> false, Curlopt_httpheader => array ());                  $params  = http_build_query ($params); $opts [curlopt_url] = $ URL: $opts [curlopt_post] = 1; $opts [curlopt_postfields] =  $params;          $ch  = curl_init (), Curl_setopt_array ($ch,  $opts); $data  = curl_ EXEC ($ch); $error  = curl_error ($ch); Curl_close ($ch);/*  Initialize and perform curl requests  */         return  $data;    }    private  Function createarraystring ($para, $encode = ' 1 ')  {        ksort ($para); reset ($para); $arg  =  "";        while  (list  ($key,  $ val)  = each  ($para))  {$arg. = $key. " =". (Isset ($encode)? UrlEncode ($val): $val). " & ";}              $arg  = substr ($arg, 0,-1); GET_MAGIC_QUOTES_GPC ()) {$arg  = stripslashes ($arg);} return  $arg;//If an escape character is present, remove escape     }    private function  Formatdata ($data) {$authstr = $this->createarraystring ($data); $data [' Sign ']=md5 ($authstr. $this->config[' Password ']);return  $data;}? >

MySQL Cloud database external calling scheme such as Baidu Cloud, Sina SAE database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.