SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Custom error message for MySQL

    Is it possible to create a custom error message for whatever error MySQL endures? I used the "or die..." on the connect variable and put my message there, but it only displays that if it can't connect to the database.

    I want it to display a message no matter what kind of error shows up. Even this error:
    mysql_fetch_array(): supplied argument is not a valid MySQL result resource...
    The reason I need this, is because I get the above error when a search is made and no results are found. Instead of this, I want to tell the user to try another search.

  2. #2
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be checking in your application for the number of rows returned by the query. If you do that, then the script doesn't even have to attempt to find some rows. I think this would be a more elegant solution to the problem.

    Check the number of rows returned using the function mysql_num_rows().
    Oh no! the coots are eating my nodes!

  3. #3
    SitePoint Guru
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The thing is, there is no number of rows when it doesn't find anything, so even if I use the $num_rows I still get an error.
    Last edited by Darin; May 16, 2003 at 23:16.

  4. #4
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Really? Because if it doesn't find anything, it's a pretty safe assumption to make that there will be no rows returned in the result set. Hence why checking the value returned by mysql_num_rows() for equaling 0 is a safe bet.

    At this stage I think it's better if we see some code with your implementation and troubleshoot that.
    Oh no! the coots are eating my nodes!

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    UK
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are receiving that error message it is telling you your query did not execute successfully on the database. You need to check the query as it is failing. Do this:
    PHP Code:
    $row mysql_fetch_array($result) or die (mysql_error()); 
    So that you can at least see what MySQL did not like about your query.

    Remember that returning 0 rows is not an error. It is in fact a valid response to query. Tim is also right in that you should be checking the number of rows returned if you wish to display a message based on an empty result set.

  6. #6
    SitePoint Guru
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by timnz
    At this stage I think it's better if we see some code with your implementation and troubleshoot that.
    Here you go, I managed to fix it so that if there are no results, there is no error message, just blank. But I still wish to display some sort of message to the user to let him know his search returned no results:
    PHP Code:
    <?php

    if (("$tablename== "Select:")) {

    print 
    "<center><b>Please select a category to search in before you can proceed with your query.</b></center><br /><br />";

    } elseif ((
    "$tablename== "$tablename")) {

    // DB Connect
    $db=mysql_connect("localhost","","")
    or die (
    "<center>Please select a category to search in before you can proceed with your query.</center><br />");    // Connect to MySQL Server.
    mysql_select_db("");                       // select database

    // Change this to reflect your table and fields
    $query "SELECT * FROM $tablename WHERE LOWER(name) LIKE LOWER('%$keyword%') OR LOWER(products) LIKE LOWER('%$keyword%') ORDER BY id DESC";

    $end utime(); $run $end $start;

    $result=mysql_query($query);
    $num_rows mysql_num_rows($result);
    if(
    $myrow=mysql_fetch_array($result))    {    
    echo 
    "Searched for <a href=\"http://dictionary.reference.com/search?q=$keyword\" class=\"link\" target=\"_blank\" title=\"Look up the word $keyword at Dictionary.com\"><u>$keyword</u></a> | Results <b>1 - $num_rows</b> of about <b>$num_rows</b>. | Search took <b>" substr($run05) . "</b> seconds.";
    echo 
    "<ol>";
    do {

    echo 
    "<li>".$myrow["name"]."<br />Country: ".$myrow["country"]."<br />Products: ".$myrow["products"]."<br /><br />";

         } while (
    $myrow=mysql_fetch_array($result));    // start loop over to output more because $myrow was set again
        
    echo "</ol><br />";

    }

    } else { 
    echo 
    "No Records Found for: <u>$keyword</u> in \"$tablename\"<br /><br /><b>Try some of the following:<br /><br />- Make sure the keyword is spelled correctly.<br />- Try a different keyword.<br />- Try a more general keyword.</b><br /><br />";

    }

    ?>
    It's a little long, but hopefully you can figure it out. You can see how it works by going to my web site and using the search feature on the top right.

  7. #7
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I kind of rearranged the logic in your script a bit. I've put comments in places where I've changed things, hopefully you can follow the changes using those.

    (P.S: I also changed a lot of your double quotes to singles, and optimised your echo's simply cos they bugged me )

    PHP Code:
    <?php 
    if ($tablename == 'Select:') { 
        print 
    '<center><b>Please select a category to search in before you can proceed with your query.</b></center><br /><br />'
    } elseif (isset(
    $tablename) && trim($tablename) != '') { 
        
    // assume that they have selected the right category, and check that it isn't empty
        // you may want to do some more checking of this value
        // DB Connect 
        
    $db mysql_connect('localhost',"","" ) or die 
            (
    '<center>Please select a category to search in before you can proceed with your query.</center><br />');
        
    mysql_select_db("" );
        
    $query "SELECT * FROM $tablename WHERE LOWER(name) LIKE LOWER('%$keyword%') OR LOWER(products) LIKE LOWER('%$keyword%') ORDER BY id DESC"
        
    $end utime(); $run $end $start
        
    $result mysql_query($query); 
        if (
    mysql_num_rows($result) == 0) {
            
    // there were no rows returned in the result set
            
    echo "No Records Found for: <u>$keyword</u> in \"$tablename\"<br /><br /><b>Try some of the following:<br /><br />- Make sure the keyword is spelled correctly.<br />- Try a different keyword.<br />- Try a more general keyword.</b><br /><br />";
        } else {
            
    // there were some rows returned in the result set
            
    echo "Searched for <a href=""\"[url=http://dictionary.reference.com/search?q=$keyword]http://dictionary.reference.com/search?q=$keyword\[/url]" class=\"link\" target=\"_blank\" title=\"Look up the word $keyword at Dictionary.com\"><u>$keyword</u></a> | Results <b>1 - $num_rows</b> of about <b>$num_rows</b>. | Search took <b>"substr($run05), '</b> seconds.'
            echo 
    "<ol>"
            while (
    $myrow mysql_fetch_array($result)) {
                echo 
    '<li>'$myrow['name'], '<br />Country: '$myrow['country'], '<br />Products: '$myrow['products'], '<br /><br />'
            }
            echo 
    '</ol><br />';
        }
    } else {
        
    // $tablename does not == 'Select:'
        // nor does it equal anything worthwhile
        // spit out an appropriate error here
    }
    ?>
    Oh no! the coots are eating my nodes!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •