简体   繁体   中英

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. 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?

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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