Page 1 of 1

Displaying search terms in a PHP Search script

Posted: Wed Sep 29, 2010 4:33 pm
by ARSR
Hey all. I was just working on a practice script for a search bar, and I was able to get the script to show how many instances there are in my corresponding database, but now I need to actually display the wording that I have inserted into the mysql database. Here's the code:

Code: Select all

<?php

/*set varibles from form */
$searchterm = $_POST['searchterm'];
trim ($searchterm);

/*check if search term was entered*/
if (!$searchterm){
        echo 'Thank You <br />'; 
}

/*add slashes to search term*/
if (!get_magic_quotes_gpc())
{
$searchterm = addslashes($searchterm);
}

/* connects to database */
@ $dbconn = new mysqli('localhost', 'pma', 'PIUPro', 'SearchBar'); 
if (mysqli_connect_errno()) 
{
 echo 'Error: Could not connect to database.  Please try again later.';
 exit;
}

/*query the database*/
$query = "select search from search where search like '%" . $searchterm . "%'";
//"select * from tablename where tablerow 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['tablerow'])." <br />";
}

/*free database*/
$dbconn->close();

?>
Can anyone help me here? I'd appreciate it. Thank you.

Re: Displaying search terms in a PHP Search script

Posted: Wed Sep 29, 2010 5:49 pm
by buckit
Don't make it more difficult than it is. just echo your variable wherever you want it to show up.

Re: Displaying search terms in a PHP Search script

Posted: Wed Sep 29, 2010 7:32 pm
by ARSR
Ok. Thank you, that was easy.