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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");  

Link to comment
Share on other sites

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");  

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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"); 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.