Web Hosting Forums

Results 1 to 6 of 6

This is a discussion on PHP/MySQL "Database Not Selected Error" in the Hosting Talk & Chit-chat forum
PHP Code: if( $this -> db_conn  =  mysql_pconnect ( "localhost" , "username" , "password" )) echo  "connected<br />" ; else echo  "not connected<br />" ; if( $this -> ...

  1. #1
    Loyal Client
    Join Date
    Jun 2006
    Posts
    2

    PHP/MySQL "Database Not Selected Error"

    PHP Code:
    if($this->db_conn mysql_pconnect("localhost","username","password"))
    echo 
    "connected<br />";
    else
    echo 
    "not connected<br />";
    if(
    $this->db mysql_select_db("jcollect_ame"))
    echo 
    "db selected<br />";
    else
    echo 
    "db not selected<br />"
    I can't seem to select a database.

    I have:
    -created the database
    -created the table
    -created the user
    -given user all rights

    I am new JPC. Am I doing something that they forbid or require changes to?

    Thanks.

  2. #2
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,570
    You do get the "connected" echo, right? If not, check whether you might have forgotten the prefix in your username and check whether persistent connections are on (you can see this in a phpinfo()). On 'my' server they are currently off. If they are on your server as well, you should first enable them in php.ini.

    Try $this->db = mysql_select_db("jcollect_ame", $this->db_conn) . It's supposed to find a normal connection (mysql_connect) on its own, but the manual doesn't say that auto-find goes for persistent connections too.

    Note that you generally don't need mysql_pconnect (persistent connection). MySQL is very fast in making connections to a local host, so there's little speed gain. On the other hand unused persistent connections lingering around do keep resources occupied that could be used by other scripts. Also, if speed is critical here, you would probably be far better of using MySQLi than using that old MySQL extension with persistent connections instead of normal ones. (MySQLi is not currently installed on 'my' server, but I think support will install it on request, if it's not already present on the new machines.)
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

  3. #3
    Yeah, I know a LOT! Vin DSL's Avatar
    Join Date
    Mar 2003
    Location
    Arizona Uplands
    Posts
    10,661
    DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

    No Guts, No Story! VinDSL 2010

  4. #4
    Loyal Client
    Join Date
    Jun 2006
    Posts
    2

    Tried several options and still coming up snake eyes.

    Quote Originally Posted by Gwaihir
    You do get the "connected" echo, right? If not, check whether you might have forgotten the prefix in your username and check whether persistent connections are on (you can see this in a phpinfo()). On 'my' server they are currently off. If they are on your server as well, you should first enable them in php.ini.

    Try $this->db = mysql_select_db("jcollect_ame", $this->db_conn) . It's supposed to find a normal connection (mysql_connect) on its own, but the manual doesn't say that auto-find goes for persistent connections too.

    Note that you generally don't need mysql_pconnect (persistent connection). MySQL is very fast in making connections to a local host, so there's little speed gain. On the other hand unused persistent connections lingering around do keep resources occupied that could be used by other scripts. Also, if speed is critical here, you would probably be far better of using MySQLi than using that old MySQL extension with persistent connections instead of normal ones. (MySQLi is not currently installed on 'my' server, but I think support will install it on request, if it's not already present on the new machines.)
    -i am connecting to the db
    -persistant connections are enabled
    -i have tried using mysql_connect with same results
    -i have tried using mysql_connect in the fucntion the db is needed with same results
    -does it matter than it is in the contructor of a class

    example:
    PHP Code:
    <?php
        
    class ame{
        
    ////////////////////////////////////////// CONTRUCTOR
            
    function ame(){
            
                if(
    $this->db_conn mysql_pconnect("localhost","username","password"))
                    echo 
    "connected<br />";
                else
                    echo 
    "not connected<br />";
                if(
    $this->db mysql_select_db("jcollect_ame"$this->db_conn))
                    echo 
    "db selected<br />";
                else
                    echo 
    "db not selected<br />";

    /* rest of the class. etc. etc.*/
    I know I've been out of programming for a bit, but this should be simple. Frustrated.

    The function that needs the db (and all other similar functions) will be in this class too.
    Last edited by lashner; 06-19-2006 at 05:53 AM.

  5. #5
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,503
    Ummm you should sanitize that username password stuff from your post.

    and turn on error reporting...
    Last edited by Ron; 06-18-2006 at 11:41 PM.

  6. #6
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    Have you tried to run this code outside of the class? In other words, doing a simple query in a stand-alone PHP script and see if it works there.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

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
  •