Jump to content

[SOLVED] Returning results using keywords search


ryanwood4

Recommended Posts

I'm using this basic code to display a list of recent articles, taken from a database. However, I'd like to change it, so it only displays links to articles with certain keywords in the title/body of the article.

 

Is this possible.

 

<?php
$con = mysql_connect("xxxx","xxxx","xxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("f1times_articles", $con);

$result = mysql_query("SELECT * FROM n2s_article ORDER BY article_id DESC LIMIT 0,10");

echo "<table class='links' border='0'>
<tr>
<th></th>
</tr>";

while($row = mysql_fetch_array($result))
  {
    $url = $row['article_url'] . "-" . $row['article_id'] . ".html";
    echo "<tr>";
    echo "<td class='link'><font face='verdana' size='1pt'>» <a href='$url'>".stripslashes($row['article_title'])."</a></td>";
    
  }

mysql_close($con);
?>

 

Example: Currently it displays the latest 10 articles. I would like it, so it displays the latest 10 articles, but only ones which contain the words: Silver, Arrow, Mercedes.

 

Any help is appreciated, as my knowledge of PHP is minimal. Thanks.

Hi ryanwood4,

 

Change your MySQL query to read:

 

result = mysql_query("SELECT * FROM n2s_article WHERE match(articletitle,articlebody) against ('Silver', 'Arrow', 'Mercedes') ORDER BY article_id DESC LIMIT 0,10");

 

Change the 'articletitle' and 'articlebody' to match the actual row names in your table.

 

Hope this helps.

Sorry, ryanwood4, I missed the $ from the beginning of the query, change it to:

 

$result = mysql_query("SELECT * FROM n2s_article WHERE match(articletitle,articlebody) against ('Silver', 'Arrow', 'Mercedes') ORDER BY article_id DESC LIMIT 0,10");

Oh, sorry, you need to add a FULLTEXT index to the required fields before the query will work with the MATCH and AGAINST operators.

 

i.e.

 

ALTER TABLE n2s_article ADD FULLTEXT(articletitle,articlebody);

 

You could always try the simple search case of:

 

SELECT * FROM n2s_article WHERE articletitle,articlebody LIKE '%Silver%' or '%Arrow%' or '%Mercedes%' ORDER BY article_id DESC LIMIT 0,10");  

No, fulltext is not required for the simpler version, try:

 

SELECT * FROM n2s_article WHERE 'articletitle,articlebody' LIKE '%Silver%' or  WHERE 'articletitle,articlebody' LIKE '%Arrow%' or WHERE 'articletitle,articlebody' LIKE '%Mercedes%' ORDER BY article_id DESC LIMIT 0,10");  

Still getting this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/f1times/public_html/mclaren_news.php on line 24

 

 

<?php
$con = mysql_connect("xxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("f1times_articles", $con);

$result = mysql_query("SELECT * FROM n2s_article WHERE 'article_title,article_content' LIKE '%Silver%' or  WHERE 'article_title,article_content' LIKE '%Arrow%' or WHERE 'article_title,article_content' LIKE '%Mercedes%' ORDER BY article_id DESC LIMIT 0,10");  

echo "<table class='links' border='0'>
<tr>
<th></th>
</tr>";

while($row = mysql_fetch_array($result))
  {
    $url = $row['article_url'] . "-" . $row['article_id'] . ".html";
    echo "<tr>";
    echo "<td class='link'><font face='verdana' size='1pt'>» <a href='$url'>".stripslashes($row['article_title'])."</a></td>";
    
  }

mysql_close($con);
?>

 

I can't think of a reason as to why it won't work though. Thanks

OK, how about:

 

SELECT * FROM n2s_article WHERE 'article_title' LIKE '%Silver, Arrow, Mercedes%' AND 'article_body' LIKE '%Silver, Arrow, Mercedes%' ORDER BY article_id DESC LIMIT 0,10");  

 

Sorry, instead of going from memory I should have just run the query myself - would have saved you a lot of time!

OK, it's probably because of the AND statement limiting the results, you can change this to OR i.e.:

 

SELECT * FROM n2s_article WHERE 'article_title' LIKE '%Silver, Arrow, Mercedes%' OR 'article_body' LIKE '%Silver, Arrow, Mercedes%' ORDER BY article_id DESC LIMIT 0,10");  

 

Or, you may only with to search the article title (or body) in which case;

 

SELECT * FROM n2s_article WHERE 'article_title' LIKE '%Silver, Arrow, Mercedes%' ORDER BY article_id DESC LIMIT 0,10");  

 

Obviously, change 'article_title' to 'article_body' if it's the artcile body only you wish to search.

 

Hope this helps.

 

 

The field needs to be unwrapped or wrapped with ticks not single suotes;

 

SELECT * FROM n2s_article WHERE 'article_title' LIKE '%Silver, Arrow, Mercedes%' ORDER BY article_id DESC LIMIT 0,10"); 

 

should be

 

SELECT * FROM `n2s_article` WHERE `article_title` LIKE '%Silver, Arrow, Mercedes%' ORDER BY `article_id` DESC LIMIT 0,10"); 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.