त्रुटि: C:\wamp64\www\online-exam-portal\code\manage-exam\_export-to-csv.php लाइन 85 पर गैर-ऑब्जेक्ट की संपत्ति ‘num_rows’ प्राप्त करने का प्रयास कर रहा हूं


मैं डीबी से छात्रों द्वारा दी गई सभी परीक्षाओं के बारे में जानकारी प्राप्त करने और डेटा को सीएसवी में निर्यात करने का प्रयास कर रहा हूं, लेकिन मुझे यह त्रुटि मिल रही है,

<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 85</th></tr>

यहां वह कोड है जो मैंने लिखा था, डेटा लाने के लिए निर्यात-टू-सीएसवी.php,

पीएचपी
  1  <?php
  2  require_once '../connect.php';
  3  require_once '../session.php';
  4  
  5  ini_set('display_errors', 1);
  6  ini_set('display_startup_errors', 1);
  7  error_reporting(E_ALL);
  8  
  9  // Check if the user is logged in
 10   //(!isset($_SESSION['user_id'])) {
 11      //Redirect or handle unauthorized access
 12      //header('Location: ../session.php')
 13     //exit();
 14  //}
 15  
 16  // Set the content-type to CSV and specify the filename for download
 17  header("Content-type: application/csv");
 18  header("Content-Disposition: attachment; filename=user_responses.csv");
 19  header("Pragma: no-cache");
 20  header("Expires: 0");
 21  
 22  // Get the user ID from the session
 23  $userID = $_SESSION['userID'];
 24  $examID = $_POST['examID'];
 25  
 26  //echo "<pre>" . $userID . "<pre/>";
 27  //exit();
 28  
 29  // Fetch user responses to questions
 30  $sql = "SELECT
 31              'Multi-choice' AS exam_type,
 32              mcq.question AS question_text,
 33              mcq.correct_answer AS correct_answer,
 34              mcqr.response AS chosen_option,
 35              mcqr.score AS marks
 36          FROM
 37              multi_choice_question mcq
 38              INNER JOIN multi_choice_response mcqr ON mcq.exam_id = mcqr.exam_id
 39                  AND mcq.question_no = mcqr.question_no
 40          WHERE
 41              mcqr.assignee_id = '$userID'
 42              AND mcq.exam_id = $examID
 43  
 44          UNION
 45  
 46          SELECT
 47              'Fill in the blank' AS exam_type,
 48              fiq.question AS question_text,
 49              fir.response AS chosen_option,
 50              fir.score AS marks
 51          FROM
 52              fill_in_question fiq
 53              INNER JOIN fill_in_response fir ON fiq.exam_id = fir.exam_id
 54                  AND fiq.question_no = fir.question_no
 55          WHERE
 56              fir.assignee_id = '$userID'
 57              AND fiq.exam_id = $examID
 58  
 59          UNION
 60  
 61          SELECT
 62              'Theory' AS exam_type,
 63              tq.question AS question_text,
 64              tr.response AS chosen_option,
 65              tr.score AS marks
 66          FROM
 67              theory_question tq
 68              INNER JOIN theory_response tr ON tq.exam_id = tr.exam_id
 69                  AND tq.question_no = tr.question_no
 70          WHERE
 71              tr.assignee_id = '$userID'
 72              AND tq.exam_id = $examID";
 73  
 74  $result = mysqli_query($conn, $sql);
 75  //echo "<pre>" . $result . "<pre/>";
 76  //exit();
 77  
 78  // Create the output file (without saving it)
 79  
 80  $dataFile = fopen('php://output', 'w');
 81  
 82  
 83  
 84  
 85  if ($result->num_rows > 0) {
 86  
 87      //if ($result) {
 88        //  if ($result ->num_rows > 0) {
 89      
 90  
 91  // Get the total possible mark
 92  $markResult = $conn->query("SELECT total_mark FROM exam WHERE exam_id = $examID");
 93  $totalMark = $markResult->fetch_assoc()['total_mark'];
 94  
 95    // Output the data
 96    while ($row = $result->fetch_assoc()) {
 97      fputcsv($dataFile, $row);
 98  // }
 99  }
100  
101      // Output the header
102      fputcsv($dataFile, array("QUESTION TYPE", "QUESTION", "CORRECT ANSWER", "CHOSEN OPTION", "SCORE(out of $totalMarks)"));
103  
104  }
105      else {
106      echo "0 results";
107  }
108  
109  // Close the file handle
110  fclose($dataFile);
111  
112  // Close the database connection
113  mysqli_close($conn);
114  ?>

मैंने क्या प्रयास किया है:

C:\wamp64 में गैर-ऑब्जेक्ट की संपत्ति ‘num_rows’ प्राप्त करने का प्रयास कर रहा हूँ

समाधान 1

समस्या यह है कि $result कुछ भी शामिल नहीं है. सबसे अधिक संभावना है कि लाइन 74 पर एसक्यूएल कॉल विफल हो गई, लेकिन आप इसे जांचना भूल गए।

समाधान 2

आपकी त्रुटि संभवतः इसलिए हो रही है क्योंकि आपकी क्वेरी निष्पादन ‘mysqli_query($conn, $sql)’ विफल हो रही है, और परिणामस्वरूप, ‘$result’ आपकी ‘num_rows’ प्रॉपर्टी वाला कोई ऑब्जेक्ट नहीं है। आपको जांचना चाहिए कि क्या आपकी क्वेरी निष्पादन सफल है, क्या गलत हो रहा है यह जांचने के लिए कुछ त्रुटि प्रबंधन जोड़ें। सुनिश्चित करें कि आपका ‘$conn’ ऑब्जेक्ट वैध है और डेटाबेस से जुड़ा हुआ है –

पीएचपी
$result = mysqli_query($conn, $sql);

if (!$result) {
    die('Error : ' . mysqli_error($conn));
}

コメント

タイトルとURLをコピーしました