我在连接数据库的类中遇到错误

时间:2014-01-03 13:23:56

标签: php mysqli

我在第14和第15号时遇到语法错误。我只是想弄清楚。任何帮助

<?php

class connect_dbase
{
    public function connection(
        $host="localhost", $user="root", $password="london", $db_name="users"
    ) {
        mysqli = new mysqli($host,$user,$password,$db_name)
        if(!$mysqli) {
            die( "connection failed :" .mysqli->connect_error());
        } else {
            echo " Database connection successful";
        }
    }
}

$connect_dbase=new connect_dbase();
$connect_dbase->connection();

?>

1 个答案:

答案 0 :(得分:2)

您的代码存在一些问题。

这一行末尾缺少一个分号:

$mysqli = new mysqli($host,$user,$password,$db_name)

此外,检查数据库连接是否成功的条件语句,即使输入了错误的凭据,也始终回显“Database connection successful”。

您现在可以使用以下内容:

<?php

class connect_dbase
{
    public function connection(
        $host="localhost", $user="root", $password="london", $db_name="users"
    ) {
        $mysqli = new mysqli($host,$user,$password,$db_name);
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}


else {
            echo " Database connection successful";
        }
    }
}

$connect_dbase=new connect_dbase();
$connect_dbase->connection();

?>