Below is my current code. It populates the page but I get \"no database selected
ID: 3556318 • Letter: B
Question
Below is my current code. It populates the page but I get "no database selected". I need to know what code and missing and where it should be inserted.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?php
SESSION_START();
//quiz questions page
//connect to mysql server
$connection = mysqli_connect("localhost", "root", "", "quizproject");
if (mysqli_connect_errno($connection)) //throw error if connection not made
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
</head>
<body>
<html>
<h1>Hello, <?php echo $_SESSION["fname"]; " " ?> <?php echo $_SESSION["lname"]; ?>.</h1>
<p><h2>Quiz Time! Please answer each of the following questions:</h2><br>
<article>
<form id="quizquestions" method="post" action="Project3_Page2.php">
<?php
$arrCount = "";
if(isset($_GET['question'])){
$question = preg_replace('/[^0-9]/', $_GET['question']);
$output = "";
$answers = "";
$q = "";
$sql = mysql_query("SELECT id FROM questions");
$numQuestions = mysql_num_rows($sql);
if(!isset($_SESSION['answer_array'])|| $_SESSION['answer_array'] < 1){
$currQuestion = "1";}
else{
$arrCount = count($_SESSION['answer_array']);
if($arrCount > $numQuestions){
unset($_SESSION['answer_array']);
header("location: Project3_Page1.php");
exit();
}
}
if($arrCount >= $numQuestions){
echo "You've answered all questions!";}?>
<form id="sumbitquiz" method="post" action="Project3_Page3.php">
<input type="submit" value="SUBMIT">
</form>
<?php
exit();
}
$singleSQL = mysql_query("SELECT * FROM questions WHERE id='question' LIMIT 1");
if($singleSQL === FALSE) { die(mysql_error()); }
while($row = mysql_fetch_array($singleSQL)){
$id = $row['id'];
$thisQuestion = $row['question'];
$question_id = $row['question_id'];
$q = '<h2>'.$thisQuestion.'</h2>';
$sql2 = mysql_query("SELECT * FROM answers WHERE question_id='$question' ORDER BY rand() LIMIT 5");
while($row2 = mysql_fetch_array($sql2)){
$answer = $row2['answer'];
$correct = $row2['correct']; ?>
<?php>$answers = ?><form><label><input type="radio" name="radios" value='$correct'> <?php $answer ?></label>
<input type="hidden" id="qid" name="qid" value='.$id.'></form> </br></br>;
<?php
$output= $q && $answers ?><span id="btnSpan"><button>Submit</button></span>;
<?php echo $output;
}
}
?>
</form>
</html>
</body>
Explanation / Answer
instead of
$connection = mysqli_connect("localhost", "root", "", "quizproject");
make the connection variable like this
$connection = mysqli_connect("localhost", "root", "");
$db = mysql_select_db( $connection, "quizproject");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.