Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching multiple fileds on a database 1

Status
Not open for further replies.

je150

IS-IT--Management
Jun 10, 2003
33
0
0
US
Is it possible to create a search that allows you to identify which field of a database you want to search through. I already created a working search that looks through the field name and produces the results i need. Is there some easy way to adapt this to my needs or should i start tampering with it myself and do another 2 hour trial and error. Id like to set a variable to which field it searches through and have that be user defined while someone is using the site. Here is what i have coded so far. The field is defined at the very top of the document
Code:
<?php
// get the query for the search engine
$var = @$_GET['q'] ;  (if applicable)
//trim whitespace from the stored 
$trimmed = trim($var); variable
include 'include/include_dbconnect.php';
ConnectToDatabase();
[COLOR=red]$field_to_search = "name";
$query = "SELECT * FROM $dbtable WHERE $field_to_search LIKE \"%$trimmed%\" order by id";[/color]

include 'include/include_common.php';

?>

<html>
<head>
<title><?=$company?> - <?=$appName?></title>
</head>
<body <?=$bodyTagAttribs?> >
<center>

<?php


// Put in the header //
include '../includes/include_header.php';

// Print top caption for table
echo "<br><CAPTION ALIGN=top><FONT size=4 color=\"white\">Insert information about this site later.</FONT></CAPTION><br>";

// Begin Search Engine Code
print("<form name=\"search\" method=\"GET\" action=\"$PHP_SELF\">");
print("<font color=\"white\">Seach the Database for: </font>");
print("<input type=\"text\" name=\"q\">");
print("<input type=\"submit\" name=\"search\" value=\"Search\">");
print("</form>");


if ($trimmed == "")
{
echo "<p><font color=\"white\">Please enter a search</font></p>";
exit;
}
 // check for a search parameter
if (!isset($var))
{
echo "<p><font color=\"white\">We dont seem to have a search parameter!</font></p>";
exit;
}

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p><font color=\"white\">Sorry, your search: " . $trimmed . " returned zero results</font></p>";
}

// next determine if s has been passed to script, if not use 0
if (empty($s))
{
$s=0;
}

// get results
$result = mysql_query($query) or die("<font color=\"white\">Couldn't execute query</font>");

if($numrows > 1){ $return = "results";}
else{ $return = "result"; }

// display what the person searched for
echo "<p><font color=\"white\">Your search for " . $var . " returned $numrows $return.</font></p>";

// begin to show results set
$count = 1 + $s ;

//Sets up table format
  echo "<table WIDTH=100% BORDER=\"1\" CELLPADDING=\"1\" bgcolor=\"" . $gridBGColor1 . "\">";

  echo "<tr><th>Name</th><th>Month</th><th>Day</th><th>Year</th><th>Hours</th><th>Project ID</th><th>Description / Comments<th></tr>\n";

while ($r= mysql_fetch_array($result))
{
$name = $r["name"];
$month = $r["month"];
$day = $r["day"];
$year = $r["year"];
$hours = $r["hours"];
$pid1 = $r["pid1"];
$pid2 = $r["pid2"];
$comment = $r["comment"];

$count++ ;



// Sets up format for data presentation
printf("<tr><td valign=\"top\">%s</td><td valign=\"top\">%s</td><td valign=\"top\">%s</td><td valign=\"top\">%s</td><td valign=\"top\">%s</td><td width=75  valign=\"top\">%s-%s</td><td>%s</tr>\n", $r["name"], $r["month"], $r["day"], $r["year"], $r["hours"], $r["pid1"], $r["pid2"], $r["comment"]);

}

echo "</table>\n";

// Put in Footer
include '../includes/include_footer.php';

?>
 
Are all of you afraid of this post?? I thought it was a simple question. I just want to query different fields on a database. Maybe even compound the queries. If anyone knows even a MySQL command for that it would be useful.
 
you could do if statements for each one....like if I build checkboxes that are set:
the html:
Code:
<input type="checkbox" name="databasefield1">
<input type="checkbox" name="databasefield2">
the php:
Code:
//set your default field here:
$field_to_search="whatever";

$query = "SELECT * FROM $dbtable WHERE $field_to_search LIKE \"%$trimmed%\"";

//add on here
if(isset($_POST['databasefield1'])){
$query .= " OR ".$_POST['databasefield1']." LIKE \"%".$trimmed."%\"";
}

if(isset($_POST['databasefield2'])){
$query .= " OR ".$_POST['databasefield2']." LIKE \"%".$trimmed."%\"";
}

//finish the statement here
$query .= " ORDER BY id";



___________________________________
[morse]--... ...--[/morse], Eric.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top