Results 1 to 4 of 4

Thread: [RESOLVED] Displaying No Results Found from Search

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    17

    Resolved [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!
    ?>

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    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

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Displaying No Results Found from Search

    Use is_resource() and mysql_num_rows() to check if the query returned any results.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    17

    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
  •  



Click Here to Expand Forum to Full Width