Site Search Engine

PHP code for site search engine

Page 1 of 1

1 Replies - 1278 Views - Last Post: 11 April 2010 - 12:31 PM Rate Topic: -----

#1 romeyb1105  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-February 09

Site Search Engine

Posted 11 April 2010 - 11:29 AM

The following code is in PHP for the purpose of creating a site search engine:

<?php # Script 16.5 - index.php
// This is the main page for the site.

// Include the configuration file:
require_once ('includes/config.inc.php'); 

// Set the page title and include the HTML header:
$page_title = 'Welcome to this Site!';
include ('includes/header.html');

// Welcome the user (by name if they are logged in):
echo '<h1>Welcome';
if (isset($_SESSION['first_name'])) {
	echo ", {$_SESSION['first_name']}!";
}
echo '</h1>';
$words = "search";
// Show the search form:

echo '<form action="index.php" method="get" accept-charset="utf-8">
<p><em>' . $words . '</em>: <input name="terms" type="text" size="30" maxlength="60" ';
	
// Check for existing value:
if (isset($_GET['terms'])) {
	echo 'value="' . htmlspecialchars($_GET['terms']) . '" ';
}
//echo ($_GET['terms']);
// Complete the form:
echo '/><input name="submit" type="submit" value="' . $words . '" /></p></form>';
 require_once('mysqli_connect.php');
if (isset($_GET['terms'])) { // Handle the form.

	// Clean the terms:
	$terms = mysqli_real_escape_string($dbc, htmlentities(strip_tags($_GET['terms'])));

	// Run the query...
	$q = "SELECT * FROM `metadata` WHERE `keyword` LIKE '$terms'";
  $r = mysqli_query($dbc, $q);
	if (mysqli_num_rows($r) > 0) {
		echo '<h2>Search Results</h2>';
		$row = mysqli_fetch_row($r);
		echo ($row[0]);
		}
		
	$qq =  "SELECT location From datalocation Where `keywordID` = $row[0]";
	$rr = mysqli_query($dbc, $qq);
	 if(mysqli_num_rows($rr) > 0) 
  {
    $results = mysqli_fetch_row($rr);
    echo ($results[0]);
  } else {
	 
		echo '<p>No results found.</p>';
	}                                     
 //}
 }


?>
<p>data results</p>
<?php // Include the HTML footer file:
include ('includes/footer.html');
?>



This is for a project I'm doing in my software engineering course and when I type in the terms I have in the MySQL database now, they do pop up. However, the keyword ID pops up right next to the image and only one of the images shows up on the 1st one when there should be two.

And when I typed in a term not in the datebase to make sure I get the error message to work, it gives me the undefined variable on line #45 and I'm stuck on how I should define it. Please help.

Is This A Good Question/Topic? 0
  • +

Replies To: Site Search Engine

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,549
  • Joined: 23-August 08

Re: Site Search Engine

Posted 11 April 2010 - 12:31 PM

Let's fix your indentation:

if (isset($_GET['terms'])) { // Handle the form.
    // Clean the terms:
    $terms = mysqli_real_escape_string($dbc, htmlentities(strip_tags($_GET['terms'])));

    // Run the query...
    $q = "SELECT * FROM `metadata` WHERE `keyword` LIKE '$terms'";
    $r = mysqli_query($dbc, $q);
    if (mysqli_num_rows($r) > 0) {
        echo '<h2>Search Results</h2>';
        $row = mysqli_fetch_row($r);
        echo ($row[0]);
    }
                
    $qq =  "SELECT location From datalocation Where `keywordID` = $row[0]";
    $rr = mysqli_query($dbc, $qq);
    
    if(mysqli_num_rows($rr) > 0) {
        $results = mysqli_fetch_row($rr);
        echo ($results[0]);
    } else {
        echo '<p>No results found.</p>';
    }                                     
 }



Does that make any more sense? What is the value of $row[0] at your $qq query when you had NO ROWS returned from your $q query?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1