MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 17th, 2004, 02:48 PM
Danboy Danboy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 1 Danboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question PHP MySQL search engine problem

Hi folks,

I'm new here so please bear with me on this one.....

I've been following Mitch Harper's tutorial on Developing A Site Search Engine With PHP And MySQL, which can be found here. It works too...............sort of.

The problems I'm having are these:

1) When searching with more than one keyword (ie "mysql" and "apache") the code outputs two separate results (i.e "1 article found: MySQL is a great database"; "1 article found: Apache is pronounced A-Patchy") instead of the expected "2 articles found".

2) When the search keyword is not found I'm presented with a blank page instead of telling me no results were found.

The code I'm working on is this:

<?php
$submit = $_POST["submit"];
$keywords = $_POST["keywords"];
if(isset($submit) || isset($keywords)) {
doSearch($keywords); }
else {
getKeywords(); }
function getKeywords() {
?>
<html>
<head>
<title> Enter Search Keywords </title>
</head>
<body bgcolor="#FFFFFF">
<form name="frmKW" action="searchdocs.php" method="post">
<h1>Keyword Search</h1>
Enter keywords to search on:
<input type="text" name="keywords" maxlength="100">
<br><br><input type="submit" name="submit" value="Search">
</form>
</body>
</html>

<?php
}
function doSearch($search_keywords) {
$arrWords = explode(" ", $search_keywords);
if(sizeof($arrWords) == 0 || $search_keywords == "") {
echo "You didn't enter any keywords<br>";
echo "<a href='searchdocs.php'>Go Back</a>"; }
else { // Connect to the database
$dServer = "localhost";
$dDb = "content_1";
$dUser = "root";
$dPass = "whatever";
$s = @mysql_connect($dServer, $dUser, $dPass) or die("Couldn't connect to database server");
@mysql_select_db($dDb, $s)
or die("Couldn't connect to database");
for($i = 0; $i < sizeof($arrWords); $i++) {
$query = "select articleIds from searchWords where word LIKE '{$arrWords[$i]}'";
$result = mysql_query($query) or exit('$query failed: '.mysql_error());
if (mysql_num_rows($result) > 0) {
// Get the id's of the articles
$row = mysql_fetch_array($result);
$arrIds = explode(",", $row[0]);
$arrWhere = implode(" OR articleId = ", $arrIds);
$aQuery = "select articleId, title, left(content, 100) as summary from articles where articleId = " . $arrWhere;
$aResult = mysql_query($aQuery);
$count = 0;
$articles = array();
if(mysql_num_rows($aResult) > 0) {
while($aRow = mysql_fetch_array($aResult)) {
$articles[$count] = array ("articleId" => $aRow["articleId"], "title" => $aRow["title"], "summary" => $aRow["summary"]);
$count++;
}
}
if(isset($articles)) {
$articles = array_unique($articles);
echo "<h1>" . sizeof($articles);
echo (sizeof($articles) == 1 ? " article" : " articles");
echo " found:</h1>";
foreach($articles as $a => $value)
{
?>
<a href="article.php?articleId=<?php echo $articles[$a]["articleId"]; ?>">
<b><u><?php echo $articles[$a]["title"]; ?></u></b>
</a>
<br><?php echo $articles[$a]["summary"] . "..."; ?>
<br>
<a href="article.php?articleId=<?php echo $articles[$a]; ?>">
http://www.mysite.com/article.php?articleId=<?php echo $articles[$a]["articleId"]; ?>
</a>
<br><br>
<?php
}
}
else {
echo "No results found for '$search_keywords'<br>";
echo "<a href='searchdocs.php'>Go Back</a>";
}
}
}
}
}
?>
Apologies for posting so much code, but if anyone can see what I'm doing wrong I'd really appreciate it.

By the way, I'm running PHP 4.3.3 and MySQL 3.23.49

Many thanks,

Dan

Reply With Quote
  #2  
Old May 18th, 2004, 01:59 PM
Pheifel Pheifel is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Denmark
Posts: 174 Pheifel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 46 sec
Reputation Power: 5
Send a message via MSN to Pheifel
hmmm.... maybe, instead of using a foreloop with the $arrWord you could use a "wildguess" in the db with the $arrWord (after you have exploded it.) like this:

PHP Code:
 mysql_query("select * from db where word like '%$arrWord%'"); 


- Pheifel

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > PHP MySQL search engine problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway