Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Need Help Please, Searching using SQL and PHP

Codes here !

Moderators: macek, egami, gesf

Need Help Please, Searching using SQL and PHP

Postby kennysime » Tue May 06, 2014 7:58 pm

Here is my code. I can connect to the database but cannot return any search results.

<form action="Colorado_Search.php" method="get">
<label>Search for:
<select name="field2" id="field">
<option value="Author">Author</option>
<option value="ISBN">ISBN</option>
<option value="Title">Title</option>
<input type="text" name="keyname" />
<Select NAME="field" id="region">
<Option VALUE="Colorado Springs">Colorado Springs</option>
<Option VALUE="Denver">Denver</option>
<Option VALUE="Pueblo">Pueblo</option>
<Option VALUE="Eastern Colorado">Eastern Colorado</option>
<input type="submit" value="Search" />

//capture search term and remove spaces at its both ends if the is any
$var = @$_GET['field'] ;
$var1 = @$_GET['region'] ;
$searchTerm = trim($_GET['keyname']);

//check whether the name parsed is empty
if($searchTerm == "")
echo "Enter name you are searching for.";

//database connection info
$hostname1 = "xxxxx";
$username1 = "xxxxx";
$password1 = "xxxxx";
$database1 = "xxxxx";

//connect to the database
$dbh1 = mysql_connect($hostname1, $username1, $password1);
if (!$dbh1)
die('Could not connect: ' . mysql_error());

//MYSQL search statement
$query = "SELECT * FROM LISTING WHERE Author1_lname LIKE '%$searchTerm%' OR Author2_lname LIKE '%$searchTerm%' OR ISBN_10 LIKE '%$searchTerm%' OR ISBN_13 LIKE '%$searchTerm%' OR Title LIKE '%$searchTerm%'";

$results = mysqli_query($link, $query);

/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
$output = "";
while($row = mysqli_fetch_array($results))
$output .= "Author: " . $row['Author'] . "<br />";
$output .= "ISBN: " . $row['ISBN'] . "<br />";
$output .= "title: " . $row['title'] . "<br />";
echo $output;
echo "There was no matching record for the name " . $searchTerm;
New php-forum User
New php-forum User
Posts: 1
Joined: Tue May 06, 2014 7:55 pm

Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.