Hooking up Autocomplete to MySQL table

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Hooking up Autocomplete to MySQL table

bifter
Hi

I'm new here - trying to hook up Autocomplete to my db.

I have Autocomplete working fine. Here's my search.php.

<?php
$q = strtolower($_GET["q"]);
if (!$q) return;
$items = array(
"Quentin Tarantino"=>"1",
"Christopher Nolan"=>"2",
"Joel Coen"=>"3",
"Ethan Coen"=>"4"
);
 
foreach ($items as $key=>$value) {
    if (strpos(strtolower($key), $q) !== false) {
        echo "$key|$value\n";
    }
}
?>

I've queried a MySQL db in another script.

<?php
 
include 'config.php';
include 'opendb.php';
 
$query  = "SELECT first_names, surname, id FROM writers";
$result = mysql_query($query);
 
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "Name :{$row['first_names']} <br>" .
         "Surname : {$row['surname']} <br>" .
         "ID : {$row['id']} <br><br>";
}
 
include 'closedb.php';
?>

Now I can't figure out how I merge the two :(

How can I query my db but have the same functionality as the original search.php?

I'm getting close but don't have the PHP knowledge to pull it all together. Many thanks in advance for any pointers.