Trying to connect to myql - No database selected error
Results 1 to 11 of 11

Thread: Trying to connect to myql - No database selected error

  1. #1
    Senior Member
    Join Date
    Nov 2005
    Posts
    109

    Trying to connect to myql - No database selected error

    HI Folks,

    The code below keeps generating the error, I have a wordpress site using the same db which connects fine using exactly the same credtials. Any ideas folks?

    Many thanks



    PHP Code:
    <?php  
           
    require("config.php"); 

            
    $link mysql_connect("$database_host","$database_user","$database_pass") or die(mysql_error());
            
    mysql_select_db($database_name);
            
            
    $query "SELECT id,title,start, DATE_FORMAT(start, '%Y-%m-%d %H:%i' ) AS startDate, end, url FROM events ORDER BY startDate DESC";
            
    $result mysql_query($query) or die(mysql_error());
            
    $arr = array();
            while(
    $row mysql_fetch_assoc($result)){
                 
    $arr[] = $row
            }  
            echo 
    json_encode($arr);
    ?>

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,396
    First and foremost: Stop Using the MySQL Extension!

    What's the exact error message? Also, are you sure $database_name is defined and has a non-empty value? Finally, you never check to see if mysql_select_db() was successful.

  3. #3
    Senior Member
    Join Date
    Mar 2009
    Posts
    802
    Is there a particular reason why you're not using the WordPress Database Object since you're working with WordPress?

    Quote Originally Posted by bradgrafelman View Post
    WordPress unfortunately is built on the MySQL extension.
    Declare variables, not war.

  4. #4
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,767
    Quote Originally Posted by Bonesnap View Post
    WordPress unfortunately is built on the MySQL extension.
    One of the primary reasons why I hate wordpress and avoid if it possible.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  5. #5
    Senior Member
    Join Date
    Mar 2009
    Posts
    802
    Quote Originally Posted by Derokorian View Post
    One of the primary reasons why I hate wordpress and avoid if it possible.
    To be fair it's far too popular to just switch. They just made the minimum version of PHP version 5.0 this summer I believe. Let's face it, people just don't upgrade.
    Declare variables, not war.

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,396
    Quote Originally Posted by Bonesnap View Post
    people just don't upgrade.
    True, but if you throw enough warnings in their face ahead of time, when it comes time that change is no longer optional (e.g. the 'mysql' extension gets fully deprecated and tossed in the trash), you can just step back, smile sweetly, and shrug your shoulders saying "I told you so!"

  7. #7
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,682
    The problem? No variable interpolation in the select_db call. [edit]: which was what brad grafleman was telling you [/edit]

    You're welcome.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  8. #8
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,396
    No, I wasn't... namely because variable interpolation has nothing to do with the mysql_select_db() call...?

  9. #9
    Senior Member
    Join Date
    Mar 2009
    Posts
    802
    Quote Originally Posted by bradgrafelman View Post
    True, but if you throw enough warnings in their face ahead of time, when it comes time that change is no longer optional (e.g. the 'mysql' extension gets fully deprecated and tossed in the trash), you can just step back, smile sweetly, and shrug your shoulders saying "I told you so!"
    Heh, if only it were that easy.

    Reading the back and forth on php.net between the devs about deprecating and removing the extension in future versions was pretty interesting. I'm not quite sure what the best approach is to removing it. I think putting up warnings/notices and notices in the manual (like they have now) is the best approach for the time being. Not sure when the best time is to remove it entirely, perhaps the next major version, 6.0. I would modify the warnings/notices to say that the extension will be removed in version 6.0 rather than just saying it will be removed in a future version.
    Declare variables, not war.

  10. #10
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,637
    But considering when 6.0 is bound to be released, I'd actually claim that saying "in 6.0" or in "future version" is in an equally unprecise and distant future.

    But fear not: there's always a chance they won't port the mysql extension to quantum computers when those hit the market - and then we won't have to wait for 6.0 to stop supporting mysql_

  11. #11
    Senior Member
    Join Date
    Mar 2009
    Posts
    802
    Quote Originally Posted by johanafm View Post
    But considering when 6.0 is bound to be released, I'd actually claim that saying "in 6.0" or in "future version" is in an equally unprecise and distant future.
    The difference is though PHP versions are increasing, even if slowly. So while it is an imprecise future, version 6.0 is coming closer with every new release.
    Declare variables, not war.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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