1

嗨,我在db 类中创建了一个函数

FUNCTION DB_Class($dbname, $username, $password) {
          $this->db = MYSQL_CONNECT ('localhost', $username, $password)
           or DIE ("Unable to connect to Database Server");

          MYSQL_SELECT_DB ($dbname, $this->db) or DIE ("Could not select database");
     }

如何在连接时在此函数中显示 js警报中的错误消息*失败*

4

2 回答 2

2

尝试

<?php
    FUNCTION DB_Class($dbname, $username, $password) {
        $this->db = MYSQL_CONNECT ('localhost', $username, $password);

        if (!$this->db)
            echo '<script> alert("Connection error1"); </script>';

        if (!MYSQL_SELECT_DB ($dbname, $this->db))
            echo '<script> alert("Connection error2"); </script>';
    }
?>
于 2013-01-18T12:59:12.817 回答
0

您将不得不停止使用 die() 的连接函数。trigger_error() 转储错误而不杀死脚本:

function DB_Class($dbname, $username, $password) {

    $this->db = mysql_connect ('localhost', $username, $password) 
            or trigger_error (mysql_error());

    mysql_select_db ($dbname, $this->db) 
            or trigger_error ("Could not select database");
}

然后,在您的页面上,您需要访问数据库包装类的 $db 属性(我假设您的代码是某种客户数据库包装类的一部分):

if(!is_resource($databaseObject->db)){
    echo '<script>alert("Unable to connect to Database Server!");</script>';
}
于 2013-01-18T13:06:15.660 回答