Hi

Ive been working through this search engine tutorial on this website. http://www.devarticles.com/c/a/MySQL/Developing-A-Site-Search-Engine-With-PHP-And-MySQL/1/

The problems I'm having are these:

1) When searching with one keyword (ie "mysql") the code outputs one result instead of two, like discussed in the tutorial.

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

3) How do I display the articles within article.php

<?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="search" 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 = "database";

$dUser = "user";

$dPass = "password";

$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 = '{$arrWords[$i]}'";

$result = mysql_query($query);

 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]; ?>">

<span class="highlight">URL goes here</span>=<?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. Thank you.

    I see a couple of trouble spots in this,
    first mysql will make both your conditions fail, because it wont explode into an array via a space nor will search_keyword be "", so your search wont execute.

    I think it will be worth your time to read up on "FULL TEXT" searching as you wont have to seperate your values into an array.

    Another thing I saw is that you are exploding a row of ID's fetched from a database, which means you are using comma seperated values in a database.

    It's a common thing alot of people do in the beginning but it's a big red flag when it comes to database design as the results will get unmanageable, unpredictable, you cant easily update or delete records and it will slow your queries. Also read up on "one to many" and "many to many" relationship articles you can find all over the web.

    Also, when searching unless you really want to get specific, it's best to use a comparative function like fulltext or "Select * where A LIKE B" rather then "A = B", if you are searching whting in a field of text. You can also read up on LIKE in the mysql manual.

    Also, you are doing a query for each keyword, which will KILL your search time
    it's smarter to do JOINS on the table to minimize seeks.
    JOIN's can be tricky at first but there are numerous advantages to them.

      Thanks for your reply, im a newbie, and im totally confused on what your saying......... I just wanted to create a key word search engine to place onto my website.

        Write a Reply...