Forum Discussion
BlessingBanda
Dec 18, 2023Copper Contributor
DB query export to Csv giving error
I have a DB running on wamp server for an online exam app.i am trying to export the candidates exam profile ie all the questions and answers the candidate gave but it is giving me an error.this is the export-to-csv.php file:
```
<?php
require_once '../connect.php';
require_once '../session.php';
// Check if the user is logged in
//if (!isset($_SESSION['user_id'])) {
// Redirect or handle unauthorized access
// header('Location: ../login.php');
//exit();
//}
// Set the content-type to CSV and specify the filename for download
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=user_responses.csv");
// Get the user ID from the session
//$userID = $_SESSION['user_id'];
$examID = $_POST['examID'];
$userID = $_POST['userID'];
// Fetch user responses to questions
$sql = "SELECT q.question_text, qo.option_text, sr.chosen_option
FROM user u
INNER JOIN student_response sr ON u.user_id = sr.user_id
INNER JOIN question q ON sr.question_id = q.question_id
INNER JOIN question_option qo ON sr.option_id = qo.option_id
WHERE u.user_id = $userID";
$result = mysqli_query($conn, $sql);
// Create the output file (without saving it)
$dataFile = fopen('php://output', 'w');
if ($result->num_rows > 0) {
// Output the header
fputcsv($dataFile, array("QUESTION", "OPTION", "CHOSEN OPTION"));
// Output the data
while ($row = $result->fetch_assoc()) {
fputcsv($dataFile, $row);
}
} else {
echo "0 results";
}
// Close the file handle
fclose($dataFile);
// Close the database connection
mysqli_close($conn);
?>
and this is the Csv Error output:
<br /> |
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'> |
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: userID in C:\wamp64\www\online-exam-portal\code\manage-exam\_export-to-csv.php on line <i>39</i></th></tr> |
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> |
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr> |
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0623</td><td bgcolor='#eeeeec' align='right'>411760</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp64\www\online-exam-portal\code\manage-exam\_export-to-csv.php' bgcolor='#eeeeec'>...\_export-to-csv.php<b>:</b>0</td></tr> |
</table></font> |
<br /> |
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'> |
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Trying to get property 'num_rows' of non-object in C:\wamp64\www\online-exam-portal\code\manage-exam\_export-to-csv.php on line <i>46</i></th></tr> |
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> |
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr> |
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0623</td><td bgcolor='#eeeeec' align='right'>411760</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp64\www\online-exam-portal\code\manage-exam\_export-to-csv.php' bgcolor='#eeeeec'>...\_export-to-csv.php<b>:</b>0</td></tr> |
</table></font> |
0 results |
- BlessingBandaCopper Contributorthis is the small extract from the DB dump ;
- -- Dumping data for table multi_choice_question
INSERT INTO multi_choice_question (exam_id, question_no, question, correct_answer, a, b, c, d, mark) VALUES
-- Dumping data for table multi_choice_response
INSERT INTO multi_choice_response (exam_id, question_no, assignee_id, response, score) VALUES (39,- olafhelperBronze ContributorYour post/CSV is unreadable and I don't see anything MS SQL Server related; the topic of this forum.
Better post to a more related one.