到目前为止我有两个php文件,test.php:
<?php
include("../config_conn_fordon_db.php");
include("../config_open_db.php");
// Fix Special Characters
mysql_query("SET NAMES 'utf8'") or die(mysql_error());
mysql_query("SET CHARACTER SET 'utf8'") or die(mysql_error());
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
set_include_path($root.'/SV/bincgi/zend/library/');
require_once('/library/Zend/Db/Adapter/Pdo/Mysql.php');
require_once('/library/Zend/Search/lucene.php');
$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => 'localhost',
'username' => 'user',
'password' => 'password',
'dbname' => 'fordon_db'
));
$query="SELECT * FROM cars_db";
$result = $db->fetchAll($query, 2);
echo $result[0]['headline'];
$index = Zend_Search_Lucene::create('/hej/test', true);
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::Keyword('headline', $result[0]['headline']));
$index->addDocument($doc);
$index->commit();
?>
AND search.php:
<?php
include("../config_conn_fordon_db.php");
include("../config_open_db.php");
// Fix Special Characters
mysql_query("SET NAMES 'utf8'") or die(mysql_error());
mysql_query("SET CHARACTER SET 'utf8'") or die(mysql_error());
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
set_include_path($root.'/SV/bincgi/zend/library/');
//require_once('/library/Zend/Db/Adapter/Pdo/Mysql.php');
require_once('/library/Zend/Search/lucene.php');
/*$db = Zend_Db::factory('Pdo_Mysql', array(
'host' => 'localhost',
'username' => 'user',
'password' => 'password',
'dbname' => 'fordon_db'
));
$query="SELECT * FROM cars_db";
$result = $db->fetchAll($query, 2);
echo $result[0]['headline'];
*/
$index = Zend_Search_Lucene::open('/hej/test', true);
//$doc = new Zend_Search_Lucene_Document();
echo "Index contains {$index->count()} documents.\n";
$query = "";
$hits = $index->find($query);
echo "Search for \"$query\" returned " .count($hits). " hits.\n\n";
echo "HEADLINE: "; echo $hits->headline;
?>
我这里有两个问题,首先我根本没有得到任何结果。我不确定“标题”的值是否实际插入(或索引)应该是什么,无论如何都要检查它是否已经过它的索引?
其次,我收到此错误消息:
Notice: Trying to get property of non-object in C:\wamp\www\SV\bincgi\zend\search.php on line 30
这是search.php中的最后一行,我回显标题......
我想要做的是从mysql表中获取所有'标题'字段并将它们编入索引以便我以后可以搜索它们,但我现在只开始使用它。
由于
顺便说一下:当我在test.php中回显$ result [0] ['headline']时,我从mysql表字段中得到结果,所以它可以工作到那么远!
答案 0 :(得分:1)
$hits = $index->find($query);
返回的值是一个数组;这就是你收到错误信息的原因。
返回的值通常用于以下代码中:
foreach ($hits as $hit) {
$title = $hit->title;
$contents = $hit->contents;
...
}