Webocreation

Tuesday, February 9, 2010

Search code part 2

this code source...

<?php
/*set varibles from form */
$searchterm = $_POST['searchterm'];
Trim ($searchterm);
/*check if search term was entered*/
If (!$searchterm){
echo 'Please enter a search term.';
}
/*add slashes to search term*/
If (!get_magic_quotes_gpc())
{
$searchterm = addslashes($searchterm);
}

/* connects to database */
@ $dbconn = new mysqli('localhost', 'root', '', 'sample1');
If (mysqli_connect_errno())
{
Echo 'Error: Could not connect to database. Please try again later.';
Exit;
}
/*query the database*/
$query = "select * from sheet1 where IndexNo like '%".$searchterm."%'";
$result = $dbconn->query($query);
/*number of rows found*/
$num_results = $result->num_rows;

echo '<p>Found: '.$num_results.'</p>';
/*loops through results*/
For ($I=0; $I <$num_results; $I++)
{
$num_found = $I + 1;
$row = $result->fetch_assoc();
Echo "$num_found. ".($row['IndexNo'])." <br />";
Echo "<table border='1'>
<tr>
<th>IndexNo</th>
<th>Name</th>
<th>Marks</th>
<th>Rank</th>
</tr>";

echo "<tr>";
echo "<td>" . $row['IndexNo'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Marks'] . "</td>";
echo "<td>" . $row['Rank'] . "</td>";
Echo "</tr>";
}
Echo "</table>";
/*free database*/
$result->free();
$dbconn->close();
?>

No comments:

Post a Comment