PHP页面:没有数据库选择问题

时间:2016-08-19 14:37:32

标签: php mysql

我收到No database selected on line 13错误。这段代码是

$result = $mysqli->query($query) or die($mysqli->error." on line ".__LINE__);

数据库连接成功,但当我尝试打开" question.php"文件我收到此错误。

<?php
     // Set question number
     $number = (int) $_GET['n'];

     /*
     * Get Question
     */
     $query = "SELECT * FROM questions WHERE questions.question_number = $number";

    // Get Result

    $result = $mysqli->query($query) or die($mysqli->error." on line ".__LINE__);

    $question = $result->fetch_assoc();


    /*
    * Get Choices
    */
    $query = "SELECT * FROM choices WHERE question_number = $number";

   // Get Results
   $choices = $mysqli->query($query) or die($mysqli->error.__LINE__);

?>

更新

我的数据库连接:

<?php

    // Create connection credentials
    $db_host = 'localhost';
    $db_name = 'quizzer';
    $db_user= 'root';
    $db_pass='';

    // Create mysqli object

    $mysqli = new mysqli($db_host, $db_user,$db_pass,$db_pass);

    // Error handler
    if($mysqli->connect_error){
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
}

1 个答案:

答案 0 :(得分:2)

你有一个错字,写道:

$mysqli = new mysqli($db_host, $db_user,$db_pass,$db_name);

而不是:

$mysqli = new mysqli($db_host, $db_user,$db_pass,$db_pass);