简体   繁体   中英

How to show sphinx search result with php?

I installed sphinx search on localhost (wamp). I want to show results for example in one simple html table. I tried to connect with php to sphinx search, I think it done it, but when I printed the result it was 0.... but that was not true. I do not really understand that in query part I need to put the word what I want to search? I tried search in command windows, it is good working, just I do not know how to show it on web page. And I want to use mysql. So my PHP looks like this:

require ( "sphinx/api/sphinxapi.php" );
$s = new SphinxClient;
$s->setServer("localhost", 9306);
$s->setMatchMode(SPH_MATCH_ANY);
$s->setMaxQueryTime(3);

$result = $s->query("test");

var_dump($result);

Here is my mini conf file:

source src1
{
    type            = mysql

    sql_host        = localhost
    sql_user        = root
    sql_pass        =
    sql_db          = test
    sql_port        = 3306  # optional, default is 3306

    sql_query       = \
        SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
        FROM documents

    sql_attr_uint       = group_id
    sql_attr_timestamp  = date_added

    sql_query_info      = SELECT * FROM documents WHERE id=$id
}


index test1
{
    source          = src1
    path            = C:/wamp/www/sphinx/data/test1
    docinfo         = extern
    charset_type        = sbcs
}

indexer
{
    mem_limit       = 32M
}


searchd
{
    listen          = 9312
    listen          = 9306:mysql41
    log         = C:/wamp/www/sphinx/log/searchd.log
    query_log       = C:/wamp/www/sphinx/log/query.log
    read_timeout        = 5
    max_children        = 30
    pid_file        = C:/wamp/www/sphinx/log/searchd.pid
    max_matches     = 1000
    seamless_rotate     = 1
    preopen_indexes     = 1
    unlink_old      = 1
    workers         = threads # for RT to work
    binlog_path     = C:/wamp/www/sphinx/data
}

Your connection and a query might look like this:

$link = mysql_connect ( '127.0.0.1:9306', '', '' );
$db_selected = mysql_select_db ( 'main', $link );

and then normally mysql queries (as you would do them normally)

$sql = "select * from main WHERE MATCH('$queryterm') order by date desc LIMIT 0,50";
$result = mysql_query ( $sql );

Or you can retrieve some meta info if you want(see Sphinx document for the meta)

$metainfo = mysql_query('SHOW META');
while ( $meta = mysql_fetch_array ( $metainfo ) ) {
        $srchmeta[]=$meta;
//or whatever you want here
}

$s->setServer("localhost", 9306);

Thats the mysql protocol port - sphinxQL.

In your config file you have sphinx listening on 9312 with the sphinxAPI protocol. So change your setServer line to use 9312

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