Displaying search terms in a PHP Search script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ARSR
Forum Newbie
Posts: 2
Joined: Wed Sep 29, 2010 4:28 pm

Displaying search terms in a PHP Search script

Post 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.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Displaying search terms in a PHP Search script

Post by buckit »

Don't make it more difficult than it is. just echo your variable wherever you want it to show up.
ARSR
Forum Newbie
Posts: 2
Joined: Wed Sep 29, 2010 4:28 pm

Re: Displaying search terms in a PHP Search script

Post by ARSR »

Ok. Thank you, that was easy.
Post Reply