Question

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>';

}
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top