SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist elgumbo's Avatar
    Join Date
    Nov 2002
    Location
    North West, UK
    Posts
    545
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    When is a result empty?

    Hi

    I am using the following statemant to display my records, the variable $id is passed across form the previous page.

    Code:
    $ticketlist = @mysql_query( "SELECT DISTINCT Tickets.ID, AdultPrice, ChildPrice, ChildAge,
    TID, ParkName, TicketName,
    TicketDesc, FROM Tickets, TicketLookup, TicketsParks
    WHERE TicketName LIKE '%day%' AND PID=TicketsParks.ID
    AND Tickets.ID=$id AND TID=Tickets.ID" );


    The $id variable is passed across in the url like /tickets.php/23 and as the record displays fine.

    I am using the following code to display an error if the user tries to enter something like /tickets.php/dsfhjf

    Code:
    if (!$ticketlist) {
    echo("<td>" ); 
    echo("OOPS! It looks like you tried to find a ticket we don't sell!" );
    
    } else {
    
    display the rest of the page



    .
    But if the user manually enters the url like /tickets.php/757454 or /tickets.php/13 then the page displays as normal (only minus the data) even though the results do not tie in with the sql statement (TicketName LIKE %day%)

    Is there any way I can force an error message to show if the sql statement is not met?

    I have tried empty, isset and anything else I can find but without success.

    Can any tell me where I am going wrong?

    Thanks

  2. #2
    SitePoint Zealot itsyM's Avatar
    Join Date
    Jul 2002
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if I understood your problem correctly, but here goes anyway

    One way to return an error message if the query fails is to use:

    PHP Code:
    doSomething() or die('Error message here.'); 
    or

    PHP Code:
    doSomething() or exit('Error message here.'); 
    A failed query will return a false, and that triggers the exit-part:

    PHP Code:
    $ticketlist mysql_query"SELECT DISTINCT Tickets.ID, AdultPrice, ChildPrice, ChildAge,
    TID, ParkName, TicketName,
    TicketDesc, FROM Tickets, TicketLookup, TicketsParks
    WHERE TicketName LIKE '%day%' AND PID=TicketsParks.ID
    AND Tickets.ID=
    $id AND TID=Tickets.ID" ) or exit('<p>Could not execute the query:' mysql_error() . '</p>'); 
    However, if you wanted to know how to find out if no results were found that matched your query you could use:

    PHP Code:
    if (mysql_num_rows($yourResultSetHere) < 1) {
       echo(
    '<p>No results found.</p>');


  3. #3
    SitePoint Evangelist elgumbo's Avatar
    Join Date
    Nov 2002
    Location
    North West, UK
    Posts
    545
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers ItsyM

    I wanted the second solution although god knows how you worked that out from my original post


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
  •