Hello,
I have a mysql db with 2 tables, searchwords and articles. searchwords is filled with words used to search the articles tables. I want to take one word from searchwords, search for that word in articles and return the article it appears in, and delete that word from the searchwords table. if that word doesn't exist in the article, it is deleted and the next searchword is used. If there are no words in searchwords, then i want to return a random article.
I have this function
function returnArticle()
{
$searchwordQuery = mysql_query("SELECT searchword FROM searchwords LIMIT 1") or die (mysql_error());
if( mysql_num_rows($searchwordQuery) )
{
$searchwordRow = mysql_fetch_assoc($searchwordQuery);
$searchword = $searchwordRow['searchword'];
$articleQuery = mysql_query("SELECT title, description, article_link FROM rss_cache WHERE description REGEXP '.*$searchword' LIMIT 1") or die (mysql_error());
// now delete returned searchword from searchwords table
mysql_query("DELETE FROM searchwords LIMIT 1 ");
// if search returns nothing, call self recursivly
if( !mysql_num_rows($articleQuery) ) returnArticle();
$article = mysql_fetch_assoc($articleQuery);
}
else
// the searchwords table is empty, return random article
{ $articleQuery = mysql_query("SELECT title, description, article_link FROM rss_cache ORDER BY RAND() LIMIT 1") or die (mysql_error());
$article = mysql_fetch_assoc($articleQuery);
}
$_xml ="<?xml version=\"1.0\" ?><rss><article><header>".$article["title"]."</header><body>".$article["description"]."</body><link>".$article["article_link"]."</link></article></rss>";
return $_xml;
}
all seems to work ok except if there are searchwords that don't exist in the articles. when that happens, nothing is returned, so maybe the recursion is not working as i expect it to.
Does anyone have any suggestions?
Thanks,
Chuck