SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to display error when no rows available?

    Hey all

    I have a MySQL Database with a coupe of rows and a page setup to display the data in mysql. To do so, I go to www.site.com/example.php?id=2 and it iwll bring me the row 2 of data from the database.

    However, is it possible if someone enters www.site.com/example.php?id=100, it will bring out an error message if there is no id 100?

    Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is possible. Check if the SQL result is empty, that is, check if no rows were returned. Usually, if the result is empty it gives false, so an if-else might do it.

  3. #3
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,094
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    use
    PHP Code:
        if(mysql_numrows($res) == '0'){
        echo 
    'no results';
        }
        else{
    // do something
            

    What I lack in acuracy I make up for in misteaks

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lorenw View Post
    use
    PHP Code:
        if(mysql_numrows($res) == '0'){
        echo 
    'no results';
        }
        else{
    // do something
            

    That would work under the assumption you are using mysql function syntax (which is discouraged), but I'd rather see you not put the 0 in quotes and use ===
    PHP Code:
    if (mysql_num_rows($res) === 0)
    {
      
    // no results, return your error
    }
    else
    {
      
    // put what you have currently here

    If you are using mysqli, you can do one of the following
    PHP Code:
    if (mysqli_stmt_num_rows($res) === 0)
    {
      
    // no results, return your error
    }
    else
    {
      
    // put what you have currently here

    Or
    PHP Code:
    // assumption you have a $mysqliObject
    $stmt $mysqli->prepare($query)
    $stmt->execute();
    if (
    $stmt->num_rows === 0)
    {
      
    // no results, return your error
    }
    else
    {
      
    // put what you have currently here

    Then there is PDO
    PHP Code:
    $stmt $dbh->prepare('your query here');
    $stmt->execute();
    if (
    $stmt->rowCount() === 0)
    {
      
    // no results, return your error
    }
    else
    {
      
    // put what you have currently here

    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks you all, and also cpradio because I'm using PDO and helped me on this issue


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
  •