-2

Can someone help me with the following PHP/HTML coding in which I am getting the following error message.

"Warning: Undefined variable $result in C:\xampp\htdocs\view1.php on line 30

Fatal error: Uncaught TypeError: mysqli_fetch_assoc(): Argument #1 ($result) must be of type mysqli_result, null given in C:\xampp\htdocs\view1.php:30 Stack trace: #0 C:\xampp\htdocs\view1.php(30): mysqli_fetch_assoc(NULL) #1 {main} thrown in C:\xampp\htdocs\view1.php on line 30"

Below is the full coding of my page.

<? php

require_once("connection.php");
$query = " select * from student ";
$result = mysqli_query($conn,$query);

?>

<!DOCTYPE HTML>
<HTML>
    <style>
    table, th, td {
    border:1px solid black;
    }
    </style>
    <body>
        <h2>View Records</h2>

        <table style="width:100%">
        <tr>
        <td>Student ID</td>
        <td>First Name</td>
        <td>Surname</td>
        <td> Edit</td>
        <td> Delet</td>
        </tr>

        <?php

            while($row = mysqli_fetch_assoc($result))
                {
                    $StudentID = $row['student_ID'];
                    $FirstName = $row['first_name'];
                    $LastName = $row['last_name'];
                    
        ?>
            <tr>
                <td><?php echo $StudentID ?></td>
                <td><?php echo $FirstName ?></td>
                <td><?php echo $LastName ?></td>
                <td><a href="edit.php?GetID=<?php echo $StudentID ?>">Edit</a></td>
                <td><a href="delete.php?Del=<?php echo $StudentID ?>">Delete</a></td>
                </tr>
        <?php } ?>

        </table>
    </body>
</html>

All I am trying to do is get some data from my YAHUAS database table called Student as then I have some edit and delete PHP coding to work from here but have this error regarding the while($row = mysqli_fetch_assoc($result)) and can not find where the issue is.

At the top of the PHP command there is a link to another PHP called connection.php which has the following commands.

<?php
$servername = "localhost";
$username = "root";
$password = "Root";
$dbname = "Yahuas";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

?>

Thanks Adam

Adam J
  • 1
  • 1
  • 4
  • The error is pretty clear, right? It says `$result` is null, when it should not be. So look at the line which sets that variable, and think about why it might fail. Searching for your error msg also turns up plenty of answers. – Don't Panic Dec 20 '21 at 02:46
  • What debugging have you tried? Can you manually connect to the DB with a client using those details? There is a *lot* of detail in the linked duplicate question about how to trace this kind of error - have you tried any of that? – Don't Panic Dec 20 '21 at 22:08

1 Answers1

-2
  1. Make sure you can connect MySQL
// Example username:root,password:123456,db_name:RUNOOB

$con = mysqli_connect("localhost","root","123456","RUNOOB"); 
if (mysqli_connect_errno($con)) {
    echo "MySQL Error: " . mysqli_connect_error(); 
}
  1. Make sure the table student is not empty
Aldan
  • 674
  • 9
  • 23
ynwa
  • 47
  • 4
  • Thanks you YNWA, I have a php file called connection.php which has all the following MySQL connection. which I have updated on the above request, as you can see under the 'require once' command at the top of the coding. The student table doesn't have any blank fields which I am wanting to show on my view.php page – Adam J Dec 20 '21 at 10:21