使用PHP-ON-COUCH,我尝试使用PHP-ON-COUCH在couchdb中获得所有记录,但它不是快速工作。
require_once "lib/couch.php";
require_once "lib/couchClient.php";
require_once "lib/couchDocument.php";
$couch_dsn = "http://localhost:5984/";
$couch_db = "couch";
$client = new couchClient($couch_dsn,$couch_db);
$all_singers = $client->getAllDocs();
foreach ( $all_singers->rows as $row ) {
$doc = $client->getDoc($id);
echo $doc->singer;
echo $doc->title;
echo $doc->description;
}
是否有其他方法可以正确地做到这一点?
thank you in advance
您没有正确使用函数。你现在所做的是非常慢的因为你取了所有的文档然后你再用(getDoc)函数一个接一个地取它们。当您查询所有文档时,您将得到如下内容:
{
"total_rows": 0,
"count": 0,
"rows": [{
"key": 1,
"value": "value",
"doc": {
"singer": "Foo",
"title": "bar"
}
}]
}
这是你的代码的修改版本:
<?php
require_once "lib/couch.php";
require_once "lib/couchClient.php";
require_once "lib/couchDocument.php";
$couch_dsn = "http://localhost:5984/";
$couch_db = "couch";
$client = new couchClient($couch_dsn, $couch_db);
$all_singers = null;
try {
$all_singers = $client->include_docs(true)->getAllDocs();
} catch (Exception $e) {
//Handle the exception here.
}
if (!isset($all_singers) || !isset($all_singers->rows))
echo "No singers found";
else
foreach ($all_singers->rows as $row) {
if (isset($row->error))
continue; //Log the error or something like this
if (isset($row->doc)) {
$doc = $row->doc;
echo $doc->singer;
echo $doc->title;
echo $doc->description;
}
}