如何在 php 中获取 sphinx 连接失败错误

How to get sphinx connection failed error in php

这是我的代码:

    include_once( "extensions/sphinxapi.php" );
    $mode = SPH_MATCH_EXTENDED2;
    $host = "localhostt"; //am giving wrong to get error
    $port = 9312;
    $ranker = SPH_RANK_NONE;
    $ranker = SPH_RANK_PROXIMITY_BM25;

    $cl = new SphinxClient();
    $cl->GetLastError();    //this line is Not working
    $testcon=$cl->SetServer ( $host, $port );
    if($testcon){ print_r($cl); }else if(!($testcon)) {echo "show error";}
    $cl->SetConnectTimeout ( 1 );
    $cl->SetArrayResult ( true );
    $cl->SetWeights ( array ( 100,50,100,100,100,100,100,100,100,100,100,100,100,100,100));
    $cl->SetMatchMode ( $mode );
    $cl->SetSortMode(SPH_SORT_EXTENDED, $sortby);
    $cl->SetRankingMode ($ranker);
    $cl->ResetFilters();

sphinx 正常工作。这不是问题。但是如果我给主机名错误的名字意味着它不会显示任何错误。

$host = "localhostt";//like this
$c1->GetLastError(); //not working Gives empty result .
$c1->GetLastWarning(); //not working also gives empty result.

如何获取连接失败错误。我需要在 site.Thanks.

中显示错误

我不知道 Sphinx,但我认为 localhost 将用作后备。

最简单的解决方案是停止 Sphinx 守护程序并尝试重新连接。

但是,这样更好:

try
{
    // connect to Sphinx
}
catch (Exception $e)
{
   // handle the error customized!
}
  //  after running the query only it shows error

$search = $cl->Query($keywords, $indexName); 

   if ( $search===false )
     {
        print_r($cl->GetLastError());
     }

感谢您的帮助。

你可以试试

$connected = $cl->Open();

连接失败时returnsfalse

例如:

  $this->sphinxApiClient = new \SphinxClient();
  $this->sphinxApiClient->SetConnectTimeout(self::CONNECTION_TIMEOUT);
  $this->sphinxApiClient->SetMaxQueryTime(self::MAX_QUERY_TIME);
  $this->sphinxApiClient->SetServer($host, $port);
  $connected =  $this->sphinxApiClient->Open();

 if (!$connected) {
    ....
 } else {
    ....
 }