SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs down Cannot Connect to MySQL

    I have created a web site in PHP and MySQL. Things work fine normally, but very occasionally it appears that a connection with the MySQL server cannot be made and as a result of the database driven information on the page does appear by instead I get the following dispalyed whenever information from the database is needed:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/u/s/username/public_html/browse_category.php on line 64
    Is there any reason why this is happenning. It is very infrequent.

    The code on each page is $conn=db_connect(); which then calls the following connection code from an external file:
    function db_connect()
    {
    $result= @mysql_pconnect("localhost", "username", "password");
    if (!result)
    return false;
    if (!@mysql_select_db("username"))
    return false;
    return result;
    }
    Any ideas?

  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    first of all mysql_select_db() should have the database name, NOT the username. But oftentimes they are the same and the fact that it works sometimes tells me this isn'6t the real problem.

    Actually, the error tells me that it's a data problem. If there is a syntax error in the query (thus it not executing correctl and having no resulting resultset) or if the data queried results in no results, then you would get that error. Check your query for syntactical errors (try executing just the query in mysql or phpmyadmin) and go from there.

    Aaron
    Aaron Brazell
    Technosailor



  3. #3
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Sketch,

    The data thing is fine. As soon as I refresh the page, it displays fine. It is just random times when there is a problem with connecting to the database...

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Based on the way you have structured your logic, the function might occasionally think return $result is inside the if (!@mysql_select_db("username")) clause. Try this ...
    PHP Code:
    function db_connect()
    {
      
    $result= @mysql_pconnect ("localhost""username""password");
      if (!
    $result) {
        return 
    false;
      }
      if (!@
    mysql_select_db("username")) {
        return 
    false;
      }
      return 
    $result;

    And the other error in your code (unless it's a typo in here) is that you use result rather than $result.
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Addict
    Join Date
    Oct 2002
    Posts
    229
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes that was $ missing on the result is actually missing in my connection code!!!

    I have now fixed that......

    Please tell me what happens if the database connection cannot be made, ie it returns false?

    At the moment when calling the connection function i have $conn=db_connect;

    This is all I have. Should I include something like:
    if(!$conn)
    exit;

    and by including something like this would I prevent anymore code from running on that page?

    Thanks


  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's all a matter of structuring your code to provide suitable error detection. E.g. you could show an error message to say your database is offline and show some other content ...

    PHP Code:
    if (!$conn db_connect()) {
      echo (
    "<p>Database not available right now. Please try later.</p>");
    } else {
      
    //your normal page content
      // goes here

    Ian Anderson
    www.siteguru.co.uk


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
  •