Results 1 to 4 of 4

Thread: [RESOLVED] No database selected

  1. #1

    Thread Starter
    Lively Member sridharao's Avatar
    Join Date
    Feb 2007
    Posts
    106

    Resolved [RESOLVED] No database selected

    I have two databases and since there have to separate users for each database, I used this approach:
    PHP Code:
    $con1 mysql_connect("www.mysite.com","user1","password") or die(mysql_error());
    $con2 mysql_connect("www.mysite.com","user2","password") or die(mysql_error());

    mysql_select_db("db1",$con1) or die(mysql_error());
    $aquery "SELECT * FROM invest";
    $result1mysql_query($aquery) or die(mysql_error());
    $ct1 mysql_num_rows($result1);

    mysql_select_db("microx",$con2) or die(mysql_error());
    $bquery "SELECT * FROM general";
    $result2 mysql_query($bquery) or die(mysql_error());
    $ct2 mysql_num_rows($result2); 
    The error that is showing up on the site is "No database selected".
    Am I wrong in connecting to the database both at the same time or is something else wrong?
    Save trees, avoid plastics, say no to zoo, go veg, recycle as much, live holistic

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: No database selected

    mysql_query() has an optional second argument, which is the connection the query should use. If you don't specify this argument, it'll use the last one you opened - so all of your mysql_query() calls are using $con2 (and when your first query is called, you haven't selected the database for $con2 yet, hence the error: "no database selected").

    Change your calls to...
    mysql_query($aquery,$con1)
    mysql_query($bquery,$con2)
    Last edited by SambaNeko; Jul 17th, 2009 at 11:33 AM.

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: No database selected

    PHP Code:
    // Database connection variables
    $dbDatabase "db1";
    //connect to server or exit
    if (!($conn mysql_connect("www.mysite.com","user1""password") )){
    echo 
    'result=connection+failed';
    exit;
    }
    //connect to database or exit
    if (!(mysql_select_db($dbDatabase$conn))){
    echo 
    'message=db+selection+failed';
    exit;

    Specify the database at the top of the php file so the code knows what you are referring to.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4

    Thread Starter
    Lively Member sridharao's Avatar
    Join Date
    Feb 2007
    Posts
    106

    Resolved Re: No database selected

    Thanks SambaNeko, that is exactly a type of solution I was looking for.
    Save trees, avoid plastics, say no to zoo, go veg, recycle as much, live holistic

Posting Permissions

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



Click Here to Expand Forum to Full Width