<?php

#====================
# For more examples see: http://www.nearby.org.uk/sphinx/search-example.php

# Change this settings to match your setup... 

$CONF = array();

$CONF['sphinxql_host'] = '127.0.0.1'//use an IP address, to avoid delays in DNS lookup
$CONF['sphinxql_port'] = 9306//this demo uses the SphinxQL interface (called mysql41 in the listen directive)

$CONF['mysql_host'] = "localhost";
$CONF['mysql_username'] = "user";
$CONF['mysql_password'] = "password";
$CONF['mysql_database'] = "data";

$CONF['sphinx_index'] = "myindex";

#mysql query to fetch results needs id, title and body columns
#<ids> is replaced by the list of ids
#this query can be as arbitary complex as required - but mysql has be able to run it quickly
#don't include a order by

$CONF['mysql_query'] = '
SELECT page_id AS id,title AS title,description AS body
FROM table
WHERE page_id IN ($ids)
'
;

#the linke for the title
$CONF['link_format'] = '/page.php?page_id=$id';

#====================

#Nothing below should need changing

$q = isset($_GET['q'])?$_GET['q']:'';

$q preg_replace('/ OR /',' | ',$q);

$q preg_replace('/[^\w~\|\(\)\^\$\?-]+/',' ',trim(strtolower($q)));

?>
        <form action="?" method="get">
        Search: 
                <input name="q" type="text" value="<? echo htmlentities($q); ?>"/>
                <input type="submit" value="Search"/>
        </form>
<?

if (!empty($q)) {

    
$qo $q;
    if (
strlen($qo) > 64) {
        
$qo '--complex query--';
    } 
    
    if (
1) {
        
//Connect to sphinx, and run the query
        
$sphinxql mysql_connect($CONF['sphinxql_host'].':'.$CONF['sphinxql_port'],'','') or die("ERROR: unable to connect to searchd");

        
$query "SELECT id FROM {$CONF['sphinx_index']} WHERE MATCH('".mysql_real_escape_string($q)."') LIMIT 25";
        
$result mysql_query($query,$sphinxql);        

        
// --------------
        
        
if ( !$result || mysql_errno($sphinxql) > )
        {
            print 
"\tQuery failed: -- please try again later.\n";
            exit;
        } else
        {
            
//We have a result to get details for
            
$result2 mysql_query("SHOW META",$sphinxql);
            
$res = array();
            while(
$row mysql_fetch_array($result2,MYSQL_ASSOC)) {
                
$res[$row['Variable_name']] = $row['Value'];
            }

            
$query_info "Query '".htmlentities($qo)."' retrieved ".mysql_num_rows($result)." of {$res['total_found']} matches in {$res['time']} sec.\n";
        }
        
        if (
mysql_num_rows($result)) {
            
//Build a list of IDs for use in the mysql Query and looping though the results
            
$ids = array();
            while(
$row mysql_fetch_array($result,MYSQL_ASSOC)) {
                
$ids[] = $row['id'];
            }
            
        } else {
            
$r "\t--none--";
        }
            
    }
    
    if (!empty(
$ids)) {
        
//Connect to mysql get rows
        
$db mysql_connect($CONF['mysql_host'],$CONF['mysql_username'],$CONF['mysql_password']);
        
mysql_select_db($CONF['mysql_database'], $db);
        
        
$sql str_replace('$ids',implode(',',$ids),$CONF['mysql_query']);
        
$result mysql_query($sql) or die ("Couldn't select query : $sql " mysql_error() . "\n");
    
        if (
mysql_num_rows($result) > 0) {
            print 
"<ol id=\"results\">";        
            
$rows = array();
            while (
$row mysql_fetch_array($result,MYSQL_ASSOC)) {
                
$rows[$row['id']] = $row;
            }
            foreach (
$ids as $c => $id) {
                
$row $rows[$id];
                
                
$link htmlentities(str_replace('$id',$row['id'],$CONF['link_format']));

                print 
"<li><a href=\"$link\">".htmlentities($row['title'])."</a><br/>";

                print 
htmlentities($row['body'])."</li>";
            }
            print 
"</ol><pre>$query_info</pre>";
        } else {
            print 
"--none--";
        }
    } 
    
}