无法选择学生:用户''@ localhost'拒绝访问数据库'student'

时间:2015-06-18 06:37:50

标签: php mysql sql-server database mysqli

  

无法选择学生:用户''@ localhost'拒绝访问   数据库'学生'

尝试运行以下代码时出错。

这里描述的方法都没有用,这就是我用过的代码

<?php
    $servername = "localhost";
    $username   = "amal";
    $password   = "ZtFnzcDQB5K9hutM";
    $dbname     = "student";
    $conn       = new mysqli($servername, $username, $password);
    // Check connection 
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    if (!mysql_select_db("student")) {
        echo "Unable to select student: " . mysql_error();
        exit;
    }
    $sql = "SELECT id as  Name, Course, DOB, Gender         FROM   application         WHERE  userstatus = 1";

    $result = mysql_query($sql);
    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();

        exit;
    }

    if (mysql_num_rows($result) == 0) {
        echo "No rows found, nothing to print so am exiting";

        exit;
    }
    while ($row = mysql_fetch_assoc($result)) {
        echo $row["id"];
        echo "  ";
        echo $row["Name"];
        echo "   ";
        echo $row["Course"];
        echo "   ";
        echo $row["DOB"];
        echo "  ";
        echo $row["Gender"];
        echo "<br><br>";
    }

    mysql_free_result($result);
    $conn->close();
?> 

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

   <?php
        $servername = "localhost";
        $username   = "amal";
        $password   = "ZtFnzcDQB5K9hutM";
        $dbname     = "student";

        $conn       = new mysqli($servername, $username, $password,$dbname);
        // Check connection 
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        if (!mysqli_select_db("student")) {
            trigger_error('Database connection failed : ' .$conn->connect_error , E_USER_ERROR);
            exit;
        }
   ?>

答案 1 :(得分:0)

您同时使用了mysqlimysql功能。使用任何一个它将解决。