|
-
May 25th, 2006, 07:11 AM
#1
Thread Starter
Junior Member
[RESOLVED] Displaying No Results Found from Search
Please help it s driving me up the wall...i ve written a database search, it works fine but i would like to add a line that says how many records the search found...something like eg 0 matches found!..because i need to be able to show that no results were found.....here's my code so far
<?require('SQL_Utils.php');
$searchTerm=@($_POST['searchTerm']);
$dbConn = getDatabaseConnection();
if(empty($searchTerm)){
echo 'Search field is empty';
}else{
$query = "SELECT * FROM TBL_info WHERE name||town LIKE '%$searchTerm%'";
$result = runQuery($query, $dbConn);
$output = wrapResult ($result);
disconnectFromDatabase($dbConn);
}
?>
then in theSQLUtils page:
<?
$dbConn = connectWithDatabase('localhost:3306', 'db', 'user', 'pass');
function connectWithDatabase($host, $database, $username, $password) {
$dbConn = mysql_connect($host, $username, $password)
or die ('Could not Connect!!: ' .mysql_error());
mysql_select_db($database);
return $dbConn;
}
function disconnectFromDatabase($dbConn) {
mysql_close ($dbConn);
}
function getDatabaseConnection() {
global $dbConn;
return $dbConn;
}
function runQuery ($SQL, $dbConn) {
return mysql_query($SQL, $dbConn);
}
function wrapResult($result) {
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<table width=450 border=2 bordercolor=#003366 align=center cellpadding=18><tr><td>Name: $row[1] <br> Address: $row[2] <br> $row[3] <br> $row[4] $row[6]<br> Zone: $row[5] <br> Telephone: $row[10] <br> Mobile: $row[11] <br> Category: $row[7] <br> Description: $row[8] <br> Products/Services: $row[10] <br> email: $row[12] <br> Url: $row[13] <br> Fax: $row[14]</td></tr></table>";
}
}
Please Help!
?>
-
May 25th, 2006, 08:02 AM
#2
Re: Displaying No Results Found from Search
I am not sure if this is the most effective solution, but you could use the mysql_num_rows function.
IE)
PHP Code:
$numResults = mysql_num_rows($result);
echo "Number of results: " . $numResults;
Also, please place [php] [/php] tags around your code, as it improves readability
HTH
-
May 25th, 2006, 08:22 AM
#3
Re: Displaying No Results Found from Search
Use is_resource() and mysql_num_rows() to check if the query returned any results.
-
May 26th, 2006, 03:30 AM
#4
Thread Starter
Junior Member
Re: Displaying No Results Found from Search
Thanks a lot problem solved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|