简体   繁体   English

如何在 PHP 中使用 sphinx 搜索?

[英]How do I use sphinx search with PHP?

I am still a programming newbie, please keep that in mind.我还是一个编程新手,请记住这一点。

I installed SphinxSearch on Linux Mint.我在 Linux Mint 上安装了 SphinxSearch。 I followed instructions from a Digital Ocean tutorial.我按照 Digital Ocean 教程中的说明进行操作。 I created a configuration file (sphinx.conf) as follows:我创建了一个配置文件(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
}

My PHP File我的 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 is throwing an error stating I am using a "Non-Object" PHP 抛出一个错误,指出我正在使用“非对象”

Any solution?有什么解决办法吗? Am I doing somethin entirely wrong?我做错了什么吗?

Most likely the problem is in问题很可能出在

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

As per http://php.net/manual/en/mysqli.construct.php when you pass 'localhost' in the 1st param mysqli tries to use unix socket, not a TCP socket and the port number therefore gets just ignored, ie you probably connect to your mysql instance instead of sphinx which then causes the problem you get.根据http://php.net/manual/en/mysqli.construct.php,当您在第一个参数中传递 'localhost' 时,mysqli 尝试使用 unix 套接字,而不是 TCP 套接字,因此端口号会被忽略,即您可能连接到您的 mysql 实例而不是 sphinx,这会导致您遇到的问题。

Try:尝试:

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

instead.反而。 Please also be aware that另请注意

echo $query->error;

is wrong since mysqli_result ( http://php.net/manual/ru/class.mysqli-result.php ) returned by query() does not have property 'error', you probably meant是错误的,因为 query() 返回的 mysqli_result ( http://php.net/manual/ru/class.mysqli-result.php ) 没有属性 'error',你可能是说

echo $conn->error;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM