I try to do a search bar for my web site, but I don't know why this code doesn't work.
Can someone give an advise, or a better idea ?
//starting php
<?php
$hostname_logon = "localhost" ;
$database_logon = "forum" ;
$username_logon = "root" ;
$password_logon = "password" ;
//open database connection
$connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unabale to connect to the database" );
//select database
mysql_select_db($database_logon) or die ( "Unable to select database!" );
// Get the search variable from URL
$var = @$_GET['q'] ;
//trim whitespace from the stored variable
$trimmed = trim($var);
//separate key-phrases into keywords
$trimmed_array = explode(" ",$trimmed);
// check for an empty string and display a message.
if ($trimmed == "") {
$resultmsg = "<p>Search Error</p><p>Please enter a search...</p>" ;
}
// check for a search parameter
if (!isset($var)){
$resultmsg = "<p>Search Error</p><p>We don't seem to have a search parameter! </p>" ;
}
if(isset($var))
{
$query = "SELECT * FROM forumtutorial_posts WHERE author LIKE '%$trimm%' OR title like '%$trimm%' OR post like '%$trimm%'" ;
// Execute the query to get number of rows that contain search kewords
$numresults=mysql_query($query);
$numresults = mysql_query ($query) or die ( "Couldn't execute query" );
while( $row= mysql_fetch_array($numresults));
{
print("$row[0] and $row[1] and $row[2]");
}
}
else
{
?>
<form action="{$_SERVER['PHP_SELF']}" method="get" name="search">
<div align="center">
<input name="q" type="text" value=" " size="15">
<input name="search" type="submit" value="Search">
</div>
</form>
<?php
}
?>
search
Moderator: General Moderators
Re: search
When your code isn't working, and you're doing a database query, it helps to echo the sql and see what you're sending to the database... putting "echo $query;" after the bit where you've created the query. In this case if you did that you'd see it's not working because your search strings are all empty because you've used $trimm rather than $trimmed.
Re: search
Thank you for the advise. I guess you had right, with the query, but I still need to put more in this script to have a search bar and I don't know which one is the next step in my code. Do you know what should I do more?