Web Hosting Forums

Results 1 to 8 of 8

This is a discussion on newbie - err 1046, no db selected in the Hosting Talk & Chit-chat forum
(This is the continuation of this post - I started a new one because the subject wasn't clear. I edited it but it doesn't show ...

  1. #1
    Loyal Client
    Join Date
    Nov 2003
    Posts
    39

    newbie - err 1046, no db selected

    (This is the continuation of this post - I started a new one because the subject wasn't clear. I edited it but it doesn't show up in the list of threads)

    I am in the process of activating SSH on my account.

    I can tell you that I connect OK to the db with PHP. In fact, I changed the username to a non-existent user to make sure connecting failed, and it does.

    While waiting for SSH, I used myphpAdmin and in the SQL interface type use dbname and it works bt of course, I'm already in that database with I run the sql command. I also tried it with a non existent db and it fails as expected.

    Anyone have any ideas on this?

    Thanks.

  2. #2
    Loyal Client
    Join Date
    Nov 2003
    Posts
    39
    OK, I got SSH activated and tried theh command use dbname and it worked fine. I even did a desc on a table to make sure I was really usng that db, and it worked as well.

    Any ideas?

  3. #3
    Ron
    Guest
    Maybe best if you publish the code you're using (substituting the specifics (pw, username, dbname). Gotta be in the code if you can connect just fine in SSH.

  4. #4
    Ron
    Guest
    Better yet -- try this code:
    Code:
    <?php
    $dbhost = "localhost";
    $dbname = "<JAGusername_database>";
    $dbuser = "<JAGusername_dbUsername>";
    $dbpasswd = "<dbUserPassword>";
    
    $db_connect_id = mysql_connect($dbhost, $dbuser, $dbpasswd);
    if($db_connect_id)
    {
            print "connected<br>";
            $dbselect = mysql_select_db($dbname);
            if(!$dbselect)
            {
                    print "Database not selected <br>";
            }
            else
            {
                    print "Database selected <br>";
            }
            mysql_close($db_connect_id);
    }
    else
    {
            print "not Connected <br>";
    }
    ?>
    EDIT: Fixed for consistency in substitution syntax in declarations.

  5. #5
    Loyal Client
    Join Date
    Nov 2003
    Posts
    39
    Thanks, Ron. It was a problem of scope. I had no idea that global variables weren't visible within functions.

    Now it works.

  6. #6
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    In order to use a global variable in a function you have to declare that they should be available in the function. To do that, use the global keyword:

    PHP Code:
    function funName($arg1$arg2, ..., $argx) {
       global 
    $globalVar1$globalVar2, ..., $globalVarX;

    or
    PHP Code:
    function funName($arg1$arg2, ..., $argx) {
       global 
    $globalVar1;
       global 
    $globalVar2;
       ...
       global 
    $globalVarX;

    Of course, "good" programming standards discourage the use of globals because they are harder to keep track of in larger projects. But once in a while they come in handy.

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

  7. #7
    Yeah, I know a LOT! Vin DSL's Avatar
    Join Date
    Mar 2003
    Location
    Arizona Uplands
    Posts
    10,661
    Originally posted by jason
    Of course, "good" programming standards discourage the use of globals because they are harder to keep track of in larger projects. But once in a while they come in handy.

    --Jason
    Funny you should mention that...

    I turned off the 'globals' on my Nuke site a while back, when 'we' went to MySQL 4.x, and it's been running fine. I haven't found a need for them at all. Live 'n' learn, eh what?
    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

  8. #8
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,570
    Turned off the globals??

    Do you mean the register_globals directive? If so, that's quite a different - security related - matter.
    Regards,

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

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
  •