doucao8982 2013-03-23 16:19
浏览 111
已采纳

php mysql_select_db无法正常工作[关闭]

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

}
  • 写回答

1条回答 默认 最新

  • dtdfj08626 2013-03-23 16:21
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?