Click to See Complete Forum and Search --> : [RESOLVED] PHP Search issues


mrtom100
02-17-2007, 05:42 PM
right i am using php to connect to mysql database i can get the results but i am unsure on how to make them display on a seperate page below is the code used for the search, i have the keyword entered by a form and then changed into a variable heres the code

<?

$new = $_POST['search'];
// connection to database - user name and password
$dbServer=mysql_connect ("localhost","cp1079_student","");
// if connection is not made prints error
if (!isset($dbServer))
{
print ("failed to connect to server");
exit;
}
// selects the database that will be used
mysql_select_db("cp1079",$dbServer);

// this removes the white space in $tnew
$tnew = trim($new);
// selects the fields to display and the table to which the search will look in and which field the search will look up the keyword in.
$sql =("SELECT title, author, artist, yearpublished, publisher FROM comics WHERE title LIKE \"%$tnew%\"");

// gives the result a varible name $queryResult
$queryResult = mysql_query($sql);
if (mysql_error())
{
echo "Problem with Query<BR>";
echo "The following error message was returned from MySQL:<BR>";
echo mysql_error();
exit;
}
// if there are no results print the message
if (mysql_num_rows($queryResult)==0)
{
echo "No records exist containing the word $new";
}
//if there are more than 0 results print message and list results
if (mysql_num_rows($queryResult)>0){
print "We have found ".mysql_num_rows($queryResult)." results with ".$new." in the title.<BR><BR>";
}
// for each record displayed show title, artist, yearpublished, publisher
while ($dbRecord=mysql_fetch_array($queryResult))
{
echo $dbRecord["title"].", ".$dbRecord["artist"].", ".$dbRecord["yearpublished"].", ".$dbRecord["publisher"]."<BR>";
}
?>


any one got any advise