SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast doubli's Avatar
    Join Date
    Sep 2001
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mySQL query things

    Hello, I have for example

    PHP Code:
    $result mysql_query("$query") or
    die (
    "Could not run query"); 
    Which works fine however it causes me two problems

    1. If there is an error, the whole of the page fails to render after that. SO any code underneath isn't loaded - as a result my page templates dont load properly (ideally that error never appears). Is there a way of forcing the rest of the page to load?

    2. How can I check for no data being returned and return a suitable error message. Eg, "Sorry cannot find any data"
    One of lifes luxuries ...

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. If there is an error, the whole of the page fails to render after that. SO any code underneath isn't loaded - as a result my page templates dont load properly (ideally that error never appears). Is there a way of forcing the rest of the page to load?

    2. How can I check for no data being returned and return a suitable error message. Eg, "Sorry cannot find any data"
    The following piece of code takes in account of both problems.

    PHP Code:

    $result 
    mysql_query($query) or print("Could not run query"); //print instead of die()

    if (mysql_num_rows($result) == 0) echo "Sorry cannot find any data";
    else {

      
    //do other stuff



  3. #3
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    could you not change die()

    I think you can just make a sort of error function and call it ..

    So If you query and there is an error you can call a function from within the die section

    PHP Code:
    $result mysql_query("$query") or error() 
    Does anyone have any Ideas?? or maybe an include or something..

    by the way if the result is empty you won't get an error.. So only if the database is down or you used a wrong answer will you get the error.. in all other cases it will give a result..

    What I always use is just
    Code:
    if(!$result) {
    ?> 
    no data was found etcetc 
    <?
    exit;
    }
    Good luck
    the neigbours (free) WIFI makes it just a little more fun

  4. #4
    SitePoint Enthusiast doubli's Avatar
    Join Date
    Sep 2001
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by cupid


    The following piece of code takes in account of both problems.

    PHP Code:

    $result 
    mysql_query($query) or print("Could not run query"); //print instead of die()

    if (mysql_num_rows($result) == 0) echo "Sorry cannot find any data";
    else {

      
    //do other stuff


    That seems to work quite nicely, thanks!!
    One of lifes luxuries ...

  5. #5
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: could you not change die()

    Originally posted by peanuts
    I think you can just make a sort of error function and call it ..
    Yep, kevin did a tutorial on this, you have a standard include file for all errors which displays a javascript popup error message...

    http://www.webmasterbase.com/article/319
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  6. #6
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    easiest way:
    PHP Code:
    if(!$result)
    {
      echo 
    "Query failed!<br>"
      
    echo mysql_error(); // only echo this is you want to print the mysql error.

    Without die() or exit() or anything like that, the page will continue to execute.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*


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
  •