Kaan Karaca Kaan Karaca - 3 months ago 13
MySQL Question

PHP Page: No database selected issue

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();
}

Answer

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);
Comments