First of all, to encode the output as JSON, you need to build an array with the results and use json_encode() , for example:
$return = array(); while($rs = mysql_fetch_array($rsd)) { $result[] = "..."; } echo json_encode($result);
But by default, html results are escaped before being displayed in typeahead, so you won’t get the expected result, but look at the HTML codes in the list of suggestions. To create posts using custom HTML, you must use Templates, as described here .
Your $result array entries may look like to provide the fields you have in html:
$result[] = array( "iurl" => "...", "fname" => "...", "caption" => "..." );
... and then populated in the template as described.
Other: the prefetch option that you use is not a type, but a bloodhound, which is usually used with typeahead, but you must first initialize and assign typeahead as source . Look here , it's pretty simple.
The bloodhound on this part can accept fixed datasets in the form of arrays (via the local option), fixed datasets via URL (using the prefetch option) or can make requests to URLs that you, how you get the value $_GET["q"] in your PHP code. In this case, you will have to use $q in your SQL and initialize the snoop using the remote option as follows:
remote: { url: 'getvalues.php?q=%QUERY', wildcard: '%QUERY' }
You do not need to do this, as it will be filtered again on the client side by type-head.js ... it's just a matter of performance and the number of results. If there are only a few, use the prefetch search option.