如何在PHP中使用sphinx搜索?

时间:2018-02-20 01:28:00

标签: php mysql sphinx

我仍然是编程新手,请记住这一点。

我在Linux Mint上安装了SphinxSearch。我按照数字海洋教程的说明进行操作。我创建了一个配置文件(sphinx.conf),如下所示:

source mySource{
type = mysql
sql_host = localhost
sql_user = root
sql_pass = mypass
sql_db = test
sql_query = SELECT id, uname FROM users
sql_attr_uint = id
sql_attr_string = uname
sql_port = 3306
}

index test{
source = mySource
path = /var/lib/sphinxsearch/data
docinfo = extern
}
searchd{
listen = 9306:mysql41
log = /var/log/sphinxsearch/searchd.log
query_log = /var/log/sphinxsearch/query.log
read_timeout = 5
pid_file = /var/run/sphinxsearch/searchd.pid
seamless_rotate = 1
preopen_indexes = 1
unlink_old = 1
binlog_path = /var/lib/sphinxsearch/data
}

我的PHP文件

<?php 
 $conn = new mysqli('localhost','root','mypass','test',9306);

  if($conn->connect_error){
     die("Could not connect");
  }else{
     echo "You are connected!";
  }

  $query = $conn->query("SELECT * FROM test WHERE MATCH('hsmith')");
  echo $query->error;

 ?>

PHP正在抛出错误,说明我正在使用&#34;非对象&#34;

任何解决方案?我做错了吗?

1 个答案:

答案 0 :(得分:0)

问题很可能在

 $conn = new mysqli('localhost','root','mypass','test',9306);

http://php.net/manual/en/mysqli.construct.php当你通过本地主机&#39;在第一个参数中,mysqli尝试使用unix套接字,而不是TCP套接字,因此端口号被忽略,即你可能连接到你的mysql实例而不是sphinx,这会导致你得到的问题。

尝试:

 $conn = new mysqli('127.0.0.1','','','test',9306);

代替。 还请注意

echo $query->error;

是错误的,因为query()返回的mysqli_result(http://php.net/manual/ru/class.mysqli-result.php)没有属性&#39;错误&#39;,你可能意味着

echo $conn->error;