sae mysql_SAE 平台MYSQL数据库连接方式

//-------stl LIB 中给了一个数据库操作类

class sae_mysql

{

function __construct( $host , $port , $accesskey , $secretkey , $appname , $do_replication )

{

$this->host = ltrim($host, 'ms');

$this->port = $port;

$this->accesskey = $accesskey;

$this->secretkey = $secretkey;

$this->appname =$appname;  //$this->appname =  'app_' . $appname; 其实是数据库名称

$this->do_replication = $do_replication;

}

private function connect( $is_master = true )

{

if( $is_master ) $host =$this->host;  //$host ='m' .$this->host;

else $host =$this->host;         //else $host ='s' .$this->host;

if( !$db = mysql_connect( $host . ':' . $this->port , $this->accesskey , $this->secretkey ) )

{

die('can\'t connect to mysql ' . $this->host . ':' . $this->port );

}

mysql_select_db( $this->appname , $db );

return $db;

}

private function db_any()

{

}

private function db_read()

{

if( isset( $this->db_read ) )

{

mysql_ping( $this->db_read );

return $this->db_read;

}

else

{

if( !$this->do_replication ) return $this->db_write();

else

{

$this->db_read = $this->connect( false );

return $this->db_read;

}

}

}

private function db_write()

{

if( isset( $this->db_write ) )

{

mysql_ping( $this->db_write );

return $this->db_write;

}

else

{

$this->db_write = $this->connect( true );

return $this->db_write;

}

}

public function save_error()

{

$GLOBALS['SAE_LAST_ERROR'] = mysql_error();

$GLOBALS['SAE_LAST_ERRNO'] = mysql_errno();

}

public function run_sql( $sql )

{

$ret = mysql_query( $sql , $this->db_write() );

$this->save_error();

return $ret;

}

public function get_data( $sql )

{

$GLOBALS['SAE_LAST_SQL'] = $sql;

$data = Array();

$i = 0;

$result = mysql_query( $sql , $this->do_replication ? $this->db_read() : $this->db_write()  );

$this->save_error();

while( $Array = mysql_fetch_array($result, MYSQL_ASSOC ) )

{

$data[$i++] = $Array;

}

/*

if( mysql_errno() != 0 )

echo mysql_error() .' ' . $sql;

*/

mysql_free_result($result);

if( count( $data ) > 0 )

return $data;

else

return false;

}

public function get_line( $sql )

{

$data = $this->get_data( $sql );

return @reset($data);

}

public function get_var( $sql )

{

$data = $this->get_line( $sql );

return $data[ @reset(@array_keys( $data )) ];

}

public function last_id()

{

$result = mysql_query( "SELECT LAST_INSERT_ID()" , $this->db_write() );

return reset( mysql_fetch_array( $result, MYSQL_ASSOC ) );

}

public function close_db()

{

if( isset( $this->db_read ) )

@mysql_close( $this->db_read );

if( isset( $this->db_write ) )

@mysql_close( $this->db_write );

}

public function escape( $str )

{

if( isset($this->db_read)) $db = $this->db_read ;

elseif( isset($this->db_write) )    $db = $this->write;

else $db = $this->db_read();

return mysql_real_escape_string( $str , $db );

}

public function errno()

{

return     $GLOBALS['SAE_LAST_ERRNO'];

}

public function error()

{

return $GLOBALS['SAE_LAST_ERROR'];

}

}

?>

//-----------测试连接的

$host=SAE_MYSQL_HOST_M;

$port=SAE_MYSQL_PORT;

$accesskey=SAE_ACCESSKEY;

$secretkey=SAE_SECRETKEY;

$appname=SAE_MYSQL_DB;

$do_replication= true;

$mysql= new sae_mysql($host , $port , $accesskey , $secretkey , $appname ,$do_replication);

$sql = "SELECT user_name FROM test_user ";

$data = $mysql->get_data( $sql );

print_r("my_name:");

$name=current($data[0]);

print_r($name);

?>

//修改了部分连接类的写发  进行了注释

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值