PHP涔嬭璁℃ā寮忊斿缓閫犺呮ā寮(閫氳繃閫夋嫨mysql,mongo鏁版嵁搴撻摼鎺ョ被鍨嬪仛璇存槑)
1銆佷粈涔堟槸寤洪犺呮ā寮忥紵

寤洪犺呰璁℃ā寮忓畾涔変簡澶勭悊鍏朵粬瀵硅薄鐨勫鏉傛瀯寤虹殑瀵硅薄璁捐銆

2銆佺敤涓涓畝鍗曠殑mysql,mongo閾炬帴绫昏鏄庯細

<?php /**
 * 鏁版嵁搴撻摼鎺ョ被 - 姝ょ被鍙槸涓涓畝鍗曠殑璇存槑瀹炰緥锛屽闇浣跨敤锛岃鍔犱互绠鍗曞畬鍠勪慨鏀瑰悗鍦ㄤ娇鐢
 * ------------------------
 * @author liuxinming 
 * @Dtime:2012-07-02
 */ class connect{ private $localhost;//涓绘満 sql鏈嶅姟鍣 private $dbuser;//鏁版嵁搴撶敤鎴峰悕 private $password;//鏁版嵁搴撳瘑鐮 private $database;//鏁版嵁搴撳悕 private $charset;//缂栫爜 private $pconnect;//鏄惁鎸佷箙閾炬帴 private $port; //鏁版嵁搴撶鍙 private $mongo;//mongo 瀵硅薄 private $db;//db mongodb瀵硅薄鏁版嵁搴 public function __construct($config){ $this->localhost=$config['localhost']; $this->dbuser=$config['dbuser']; $this->password=$config['password']; $this->database=$config['database']; $config['charset']?$this->charset=$config['charset']:'utf8';//榛樿涓簎tf8瀛楃闆 $config['pconnect']?$this->pconnect=$config['pconnect']:0;//榛樿涓簎tf8瀛楃闆 $config['port']?$this->port=$config['port']:3360;//绔彛 //MongoDB if (!$config['option']) $config['option'] = array('connect' => true); } /**
* MYSQL杩炴帴
* @return obj
*/ public function db_connect(){ $conn = ($config['pconnect'] == 0) ? mysql_connect($this->localhost, $this->dbuser, $this->password, true) : mysql_pconnect($this->localhost, $this->dbuser, $this->password); if(!$conn){ die('Could not connect: ' . mysql_error()); } mysql_query('SET NAMES ' . $this->database, $conn); //璁剧疆鏁版嵁搴撳瓧绗﹂泦 $dbname=@mysql_select_db($this->database, $conn); if(!$dbname){ die ("Can\'t use test_db : " . mysql_error()); } return $conn; } /**
  * NoSql mongoDB閾炬帴
  */ public function mongodb(){ $server = 'mongodb://' . $this->localhost . ':' . $this->port; //閾炬帴mongodb 鏁版嵁搴 $this->mongo = new Mongo($server, $this->options); //閫夋嫨鏁版嵁搴 $this->db = $this->mongo->selectDB($this->database); //鐢ㄦ埛鍚 瀵嗙爜 $this->db->authenticate($this->dbuser, $this->password); //$mongo->connect();  //璋冪敤connect鏂规硶锛屾潵淇濇寔杩炴帴銆  //$mongo->close();  //璋冪敤close鏂规硶锛屽叧闂暟鎹簱杩炴帴銆  //$alldb= $this->mongo->listDBs(); //璋冪敤listDBs鏂规硶锛岃繑鍥$alldb杩欎釜澶氱淮鏁扮粍銆傛樉绀烘墍鏈夌殑鏁版嵁搴撱  } } /**
  * 寤洪犺呮ā寮忕被  鐩殑鏄鐞嗘暟鎹簱閾炬帴瀵硅薄鐨勫鏉傛瀯寤虹殑瀵硅薄璁捐
  * 杩欏彧鏄竴涓畝鍗曡鏄庣被锛屽闇鍒嗚涓涓畬鏁寸殑姝e紡鐜涓嬬敤鐨勬暟鎹簱澶氱绫诲瀷閾炬帴绫伙紝璇风◢寰姞浠ヤ慨鏀瑰嵆鍙敤銆
  * 褰撶劧姝ょ被浣犱篃鍙互鐩存帴鎷垮幓鐢紝鍙槸鎰熻涓嶅畬缇庛
  */ class MysqlDb{ protected $obj; protected $sqltype; public function __construct($config){ $this->obj=new connect($config); $this->sqltype=$config['sqltype']; } public function buildDb(){ if($this->sqltype=='mysql'){ $this->obj->db_connect(); }else{ $this->obj->mongodb(); } } } /* 鍒涘缓杩囩▼琚皝瑁呬簡锛屾柟渚垮垏鎹娇鐢╩ysql 鎴 mongo鏁版嵁搴撻摼鎺 
 */ $config = array( 'sqltype'=>'mysql', 'localhost' => '127.0.0.1', 'dbuser' => 'root', 'password' => '123456' , 'database'=>'weike', ); $sql=new MysqlDb($config); $sql->buildDb(); // 鐢╯ql鏌ヨ娴嬭瘯  $sql='select * from pre_brand'; $result =mysql_query($sql); $row=mysql_fetch_array($result); print_r($row); mysql_free_result($result);//閲婃斁涓庝箣鍏宠仈鐨勮祫婧 锛堣剼鏈墽琛屽畬姣曞悗浼氳嚜鍔ㄩ噴鏀惧唴瀛橈級 ?> 
娴嬭瘯杈撳嚭缁撴灉锛

Array ( [0] => 1 [bid] => 1 [1] => 9 [uid] => 9 [2] => 0 [like] => 0 [3] => [type] => [4] => 0 [local] => 0 [5] => 0 [date] => 0 [6] => 0 [extra1] => 0 [7] => 0 [extra2] => 0 [8] => 0 [extra3] => 0 [9] => 0 [extra4] => 0 [10] => 0.00 [extra5] => 0.00 [11] => 0.00 [extra6] => 0.00 [12] => 0.00 [extra7] => 0.00 [13] => 0.00 [extra8] => 0.00 ) 
PS锛氱敱浜庢垜鐨剈buntu鏈嶅姟鍣ㄦ病鏈夎MongoDB锛屾墍鏈夋病鏈夋祴璇昺ongodb銆傛湁瀹夎鐨勫悓蹇楀彲浠ユ祴璇曚笅銆

浠ヤ笂灏辨槸PHP涔嬭璁℃ā寮忊斿缓閫犺呮ā寮忕殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=133