-2

I have created a class called 'class.admin.php' which does some checking. I have a file that calls the class which works fine upto where it tries to select the db.

When I run mysql_select_db() or die I get the error 'No database selected'.

class.admin.php

class admin {

    ### Function that check for the connect file (if it exists)
    public function checkConnector() {
        if(file_exists(CONN)) { return true; } else { return false; }
    }

    ### Check connection to MYSQL
    public function checkConnection() {
        global $cn; if(mysql_connect()) { return true; } else { return false; }
    }

    ### Check connection to database
    public function checkDB() { 
        global $db; if(mysql_select_db()) { return true; } else { return false; } 
    }

index.php

$admin = new admin();

# Check the connect file exists
if($admin->checkConnector() === true) {

    # Check connection to MYSQL server
    if($admin->checkConnection()  === true) {

        ### Check selection of DB
        if($admin->checkDB() === true) {

            print 'Selection of database is fine.';

        } else {

            print 'Selection of database is not working.';

        }

    } else {

        print '<p>I\'m sorry, could not connect to MYSQL.</p>';

    };

} else {

    print '<p>I\'m sorry the connection file does not exist. Please install accordingly.</p>';

}
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Andy Webb
  • 121
  • 1
  • 9
  • BTW: `if (foo()) { return true; } else { return false; }`... Please abbreviate that to a sane `return foo();`. – deceze Mar 23 '13 at 16:22
  • http://php.net/manual/en/function.mysql-select-db.php expects a string (database name) as first parameter – Crisp Mar 23 '13 at 16:22
  • I don't understand negative points of this question. So why stack overflow exists? – hpaknia Mar 23 '13 at 16:29
  • @HPM It's certainly not for debugging individual user's one-off bugs. It's supposed to be a collection of recurring problems in programming. This isn't one of them. – deceze Mar 23 '13 at 17:25

1 Answers1

5

To SELECT a DB, You have to provide its name

mysql_select_db();   // wrong, which database to select?

Correct is

mysql_select_db("MyDatabaseName"); 

Reference: mysql_select_db

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • I have amended my class.admin to: if(mysql_select_db(NAME)) { return true; } else { return false; } NAME is the name of the db defined. Still no joy. – Andy Webb Mar 23 '13 at 16:24
  • But that is not so in the code you posted. There you are not giving any name of database to be selected. Does the error still remain there after the update? check if your constant value is correct inside the function – Hanky Panky Mar 23 '13 at 16:25
  • I amended the class function to use NAME a defined variable in the connect file. I didn't add it because the method that checks the connection works. The method now reads: if(mysql_select_db(NAME)) { return true; } else { return false; } But still no joy. – Andy Webb Mar 23 '13 at 16:29
  • is NAME being passed correctly to your method, if you echo it does it show correct value? – Hanky Panky Mar 23 '13 at 16:29
  • Yep, echo NAME; // prints NEWVODB – Andy Webb Mar 23 '13 at 16:31
  • @AndyWebb: What error do you get now? 'No joy' is not very decriptive. Please update your question accordingly. – Marcel Korpel Mar 23 '13 at 16:33
  • Sorry, I have changed the class methods to: ### Check connection to MYSQL public function checkConnection() { mysql_connect() or die(mysql_error()); } ### Check connection to database public function checkDB() { mysql_select_db(NAME) or die(mysql_error()); } – Andy Webb Mar 23 '13 at 16:38
  • I now get Access denied for user ''@'localhost' to database 'NEWNO' – Andy Webb Mar 23 '13 at 16:39
  • My connect file looks like:: – Andy Webb Mar 23 '13 at 16:39
  • That means now the DB select issue is resolved but your MySQL login does not have permissions to select this database. Check for user permissions, seems like you are trying a login with blank username – Hanky Panky Mar 23 '13 at 16:40
  • 2mins, i'll be back while I test :) – Andy Webb Mar 23 '13 at 16:44
  • @AndyWebb: Please don't post all that code into comments, but update your question. – Marcel Korpel Mar 23 '13 at 16:46
  • Guys, thank you for you help - sorry to have taken up your valuable time for such a simple error. I needed to change the class methods to: if(mysql_connect(HOST, USER, PASS)) { return true; } else { return false; } – Andy Webb Mar 23 '13 at 16:46