一直用着SAE的数据库非常的不错.就是不能外部使用.只能在sae内部使用,不太好.所以花了点时间写了点东西以便外部调用.代码比较简单.分为两部分.一部分为服务端.一部分为客户端.
理论上适用于所有云数据库.不过得根据具体情况进行调试
<?php //sae客户端代码 $config= array( ‘hostname‘ => ‘http://2.freedaili.sinaapp.com/server_apimysql.php‘, // 服务器地址 ‘database‘ => ‘app_freedaili‘, // 数据库名 ‘password‘ => ‘756663992‘, // 数据密钥(服务端自己设定) ); require_once ‘apimysql.class.php‘; $db = new apimysql($config); //$data=$db->version();print_r($data); $data=$db->getone(‘select * from test_api order by id desc‘);print_r($data); //$data=$db->getall(‘select * from test_api order by id desc‘);print_r($data); //$data=$db->insert("insert into test_api(name) values(‘test‘)");print_r($data); //$data=$db->update("update test_api set name=‘test1‘ where id=1");print_r($data); //$data=$db->delete("delete from test_api where where id=1");print_r($data); //$data=$db->query(‘CREATE TABLE IF NOT EXISTS `test_api` (`id` int(10) 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 { // 数据库连接参数配置 protected $resultdata; protected $error; protected $config = array( ‘hostname‘ => ‘127.0.0.1‘, // 服务器地址 ‘database‘ => ‘‘, // 数据库名 ‘password‘ => ‘‘, // 数据密钥 ); 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);/* 初始化并执行curl请求 */ 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);if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}return $arg;//如果存在转义字符,那么去掉转义 } private function formatdata($data){$authstr=$this->createarraystring($data);$data[‘sign‘]=md5($authstr.$this->config[‘password‘]);return $data;} } ?>
时间: 2024-10-14 08:46:50