-1

So I am a beginner in PHP and am using MySQL to make a simple CRUD application. I am done with making the CRUD application table and also have successfully connected it with my MySQL database. However, when I load the page on the server, I am facing errors which I am unable to debug. Here is part of code for my CRUD table in which I am facing the error (lines 57-74 of my code):

// Attempt select query execution my
                    $sql = "SELECT * FROM students";
                    if($result = $mysqli->query($sql)){
                        if($result->num_rows > 0){
                            echo "<table class='table table-bordered table-striped'>";
                                echo "<thead>";
                                    echo "<tr>";
                                        echo "<th>id</th>";
                                        echo "<th>Name</th>";
                                        echo "<th>Father's Name</th>";
                                        echo "<th>Mother's Name</th>";
                                        echo "<th>Email</th>";
                                        echo "<th>Contact</th>";
                                        echo "<th>Alt. Contact</th>";
                                        echo "<th>Alt. Contact</th>";
                                    echo "</tr>";
                                echo "</thead>";
                                echo "<tbody>";

Here is the error I am getting:

Notice: Undefined variable: mysqli in /Applications/XAMPP/xamppfiles/htdocs/student/options2.php on line 59

Fatal error: Uncaught Error: Call to a member function query() on null in /Applications/XAMPP/xamppfiles/htdocs/student/options2.php:59 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/student/options2.php on line 59
  • $mysql seems to be null – draz Apr 04 '20 at 11:51
  • If you have *have successfully connected it with my MySQL database*, then you need to have this connection as `$mysql`(in this code anyway) to pass to all of the other database methods. – Nigel Ren Apr 04 '20 at 11:52

1 Answers1

0

You're missing the most important line for database connection between PHP and MySQL. The mysqli class, Represents a connection between PHP and a MySQL database. Also, enable mysqli error reporting so you can easily find out the error. See more: https://www.php.net/manual/en/mysqli.query.php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("your_host", "your_user", "your_password", "your_database");

// Change character set to utf8
$mysqli->set_charset("utf8mb4");
Dharman
  • 30,962
  • 25
  • 85
  • 135
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103