2010-09-30 118 views
1

林開始學習獅身人面像搜索PHP APISPHINX搜索php查詢,如何顯示數組的結果?

運行此查詢

<?php 
    include('sphinxapi.php'); 
    $cl = new SphinxClient(); 
    $cl->SetServer("192.168.0.100", 9312); 
    $cl->SetMatchMode(SPH_MATCH_ANY ); 
    $result = $cl->Query("mimmi", "searchtest"); 

    if ($result === false) { 
     echo "Query failed: " . $cl->GetLastError() . ".\n"; 
    } 
    else { 
     if ($cl->GetLastWarning()) { 
      echo "WARNING: " . $cl->GetLastWarning() . " 
"; 
     } 

     if (! empty($result["matches"])) { 
      foreach ($result["matches"] as $doc => $docinfo) { 
       echo "$doc\n"; 
      } 

      print_r($result); 
     } 
    } 

    exit; 
?> 

我怎樣才能正確地顯示結果後,它顯示了這樣

2183 3262 5256 7812 838 1475 1701 6184 1816 Array ([error] => [warning] => [status] => 0 [fields] => Array ([0] => namn [1] => title [2] => identification_text [3] => id [4] => namn [5] => title [6] => identification_text [7] => description) [attrs] => Array () [matches] => Array ([2183] => Array ([weight] => 6 [attrs] => Array ()) [3262] => Array ([weight] => 6 [attrs] => Array ()) [5256] => Array ([weight] => 6 [attrs] => Array ()) [7812] => Array ([weight] => 6 [attrs] => Array ()) [838] => Array ([weight] => 4 [attrs] => Array ()) [1475] => Array ([weight] => 4 [attrs] => Array ()) [1701] => Array ([weight] => 4 [attrs] => Array ()) [6184] => Array ([weight] => 4 [attrs] => Array ()) [1816] => Array ([weight] => 2 [attrs] => Array ())) [total] => 9 [total_found] => 9 [time] => 0.001 [words] => Array ([mimmi] => Array ([docs] => 9 [hits] => 46))) 

我不輸出知道該怎麼做,我如何顯示結果。

+0

wtf? http://stackoverflow.com/questions/3821493/sphinx-search-does-searchd-has-to-run-as-a-service-to-make-it-work-with-php – user187291 2010-09-30 08:54:19

+0

對不起,我做了那個昨天的問題,但現在我不能選擇接受的答案 – Max 2010-09-30 10:21:04

回答

2

此時您想從sphinx返回ID並用它們查詢您的數據庫。

<?php 
    $IDs = implode(",",array_keys($result["matches"])); 
    $sql = "SELECT * FROM `tbl` WHERE `id` IN ($IDs) ORDER BY FIELD(`id`,$IDs)"; 
?> 
+0

它,你應該怎麼做?你自己用獅身人面像做了嗎? – Max 2010-09-30 08:15:31

+0

感謝它工作正常 – Max 2010-09-30 08:22:15

+0

很高興我可以幫助:-) – PureForm 2010-09-30 09:05:21

2

字段聲明你的/etc/sphinxsearch/sphinx.conf內將在$結果可用[「匹配」]:

sql_field_string = my_field # will be both indexed and stored 

這樣一來,就沒有必要來查詢數據庫