Forum Moderators: coopster
I'm having some trouble wrapping my head around arrays. I'm trying to use a PHP search class that returns an array of primary keys from a MySQL database that matches my search string. This is what's stated in the documentation for the search class:
"returns an array containing the primary keys of entries containing the parsed keywords. You can then use that to query your database to retrieve other information."
This last part is what has me stumped. Ok, so my result is an array of primary keys. Then do I need to make a loop to perform a separate query for each primary key returned? In other words, if 100 results are returned in the primary key array, does that mean I need to loop through that array and perform 100 separate database queries to retrieve the rest of the information I need to display each record? Ultimately I just want to display a list of titles and descriptions matching my search string. Performing tons of queries seems very inefficient, which makes me think I'm on the wrong track. Does it make sense to use an array as the WHERE condition in a query?
Thanks,
CliffR
a PHP search class that returns an array of primary keys from a MySQL database that matches my search string
Okey doke ... so you end up with an array of record ids identifying records which match the search query.
Then do I need to make a loop to perform a separate query for each primary key returned? ... Ultimately I just want to display a list of titles and descriptions matching my search string.
Is each matching record a valid result? Does each record contain the title and description you'd like to display?
If not, please expand on what you are trying to accomplish.
If so, then why not pull the record data you need while gathering ids?
select id,title,description from table where ...
is nearly as fast as
select id from table where ...
(And welcome to the forums, if not already extended elsewhere!) :)
I just stickied you the URL of the PHP class I'm trying to use. I understand what you're saying and I uderstand it would be almost as fast to include all the other columns in the original query, but I don't think the class was written this way. For me, implementing the class is one thing, but modifying it is another. I don't understand all of the author's code.
Any suggestions are appreciated. Thanks for your help and thanks for the welcome!
CliffR
In the MysqlSearch class file you referenced, you only have the option of returning one field in the result array, therefore you would need to run a second query against that result array in order to pull the data you need.
for($i=0;$<sizeof($search_results_array)-1;$i++) {
... run query against $search_results_array[$i] and get row data ...
}
You could accomplish the same thing as this by running a straight query against your database and pull the relevant fields in the process:
$findMatches=mysql_query("select id,title,description from table where (regexp ($keyword,Title)) ¦¦ (regexp ($keyword,Description)) order by id");
while($match=mysql_fetch_array($findMatches)) {
$id=$match["id"];
$Title=$match["Title"];
$Description=$match["Description"];
print("<tr><td>$id</td><td>$Title</td><td>$Description</td></tr>\n");
}
mysql_free_result($findMatches);
I'm sorry I don't have more details about modifying the class file to suit your purpose. You'd need to modify the building of the result array to make it more useful to you, and my wife tells me I need to go do the dishes! :)