How do I use sphinx search with PHP?

北战南征 提交于 2019-12-13 08:28:30

问题


I am still a programming newbie, please keep that in mind.

I installed SphinxSearch on Linux Mint. I followed instructions from a Digital Ocean tutorial. I created a configuration file (sphinx.conf) as follows:

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 
 $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"

Any solution? Am I doing somethin entirely wrong?


回答1:


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, i.e. you probably connect to your mysql instance instead of sphinx which then causes the problem you get.

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

echo $conn->error;


来源:https://stackoverflow.com/questions/48876610/how-do-i-use-sphinx-search-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!