0

I get No database selected on line 13 error. And this code line is

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

Database connected successfully but when I try to open "question.php" file I get this error.

<?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__);

?>

UPDATE!

My database connection:

<?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();
}
6
  • 8
    Show where you set up your database connection! Commented Aug 19, 2016 at 14:38
  • Just updated my question. Commented Aug 19, 2016 at 14:43
  • 4
    $mysqli = new mysqli($db_host, $db_user,$db_pass,$db_pass); 2 instances of $db_pass?? really?? $mysqli = new mysqli($db_host, $db_user,$db_pass,$db_name); perhaps Commented Aug 19, 2016 at 14:45
  • Just kill me please. Thanks. Commented Aug 19, 2016 at 14:47
  • 1
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST or $_GET data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Using intval to scrub values is not a general purpose solution. Commented Aug 19, 2016 at 15:29

1 Answer 1

2

You have a typo, write that :

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

instead of :

$mysqli = new mysqli($db_host, $db_user,$db_pass,$db_pass);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.