在windows上安装CouchDB数据库,并使用php连接测试

1.首先通过此链接http://cloud.github.com/downloads/juranki/couchdb/setup-couchdb-1.1.0acff79d0-git.exe下载windows的安装包

2.下载完成双击运行安装,即可

3.在到开始菜单中 Apache CouchDB-》Start CouchDB ,开启数据库

4.可以使用下列php代码进行连接和数据操作测试。

<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<?php
set_time_limit(0);
class CouchSimple {
    function CouchSimple($options) {
       foreach($options AS $key => $value) {
          $this->$key = $value;
       }
    } 
   
   function send($method, $url, $post_data = NULL) {
      $s = fsockopen($this->host, $this->port, $errno, $errstr); 
      if(!$s) {
         echo "$errno: $errstr\n"; 
         return false;
      } 

      $request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n"; 

      if ($this->user) {
         $request .= "Authorization: Basic ".base64_encode("$this->user:$this->pass")."\r\n"; 
         $request .= "\r\n"; 
      }

      if($post_data) {
         $request .= "Content-Length: ".strlen($post_data)."\r\n\r\n"; 
         $request .= "$post_data\r\n";
      } 
      else {
         $request .= "\r\n";
      }

      fwrite($s, $request); 
      $response = ""; 

      while(!feof($s)) {
         $response .= fgets($s);
      }

      list($this->headers, $this->body) = explode("\r\n\r\n", $response); 
      return $this->body;
   }
}

$pagestartime=microtime(); 

$options['host'] = "localhost"; 
$options['port'] = 5984;
// See if we can make a connection
$couch = new CouchSimple($options); 
$resp = $couch->send("GET", "/"); 
var_dump($resp); 
echo "<br/>";

// Get a list of all databases in CouchDb 
$resp = $couch->send("GET", "/_all_dbs"); 
var_dump($resp); 
echo "<br/>";

// Create a new database "test"
$resp = $couch->send("PUT", "/test"); 
var_dump($resp);
echo "<br/>";

// Get a list of all databases in CouchDb 
$resp = $couch->send("GET", "/_all_dbs"); 
var_dump($resp); 
echo "<br/>";

// Get all documents in that database
$resp = $couch->send("GET", "/test/_all_docs"); 
var_dump($resp); 
echo "<br/>"; 

for($i=0;$i<1000;$i++){
	// Create a new document in the database test with the id 123 and some data
	$resp = $couch->send("PUT", "/test/".$i, '{"id":"'.$i.'","data":"data_'.$i.'"}'); 
	var_dump($resp); 
	echo "<br/>";
}   

// Get all documents in test again, seing doc 123 there
$resp = $couch->send("GET", "/test/_all_docs"); 
var_dump($resp); 
echo "<br/>"; 

// Get back document with the id 123
$resp = $couch->send("GET", "/test/888"); 
var_dump($resp); 
echo "<br/>";

// Delete our "test" database
$resp = $couch->send("DELETE", "/test/"); 
var_dump($resp); 
echo "<br/>"; 

$pageendtime = microtime(); 
$starttime = explode(" ",$pagestartime); 
$endtime = explode(" ",$pageendtime); 
$totaltime = $endtime[0]-$starttime[0]+$endtime[1]-$starttime[1]; 
$timecost = sprintf("%s",$totaltime); 
echo "页面运行时间: $timecost 秒"; 

?>

转载于:https://my.oschina.net/wangwang110/blog/13855

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值