-4

I would be really grateful if someone can help me solve this. I just want the data to be displayed from the database, but this error keeps on happening.

The variable is Null, I have checked it using error reporting functions.

Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, null given in C:\xampp\htdocs\Train\index.php:100 Stack trace: #0 C:\xampp\htdocs\Train\index.php(100): mysqli_query(NULL, 'select * from t...') #1 {main} thrown in C:\xampp\htdocs\Train\index.php on line 100

This is the code:

<?php include("connection.php");?>


<!--Scheduler-->

        <div class="callout border-primary rounded-0 shadow">
            <fieldset>
                <section class="schedule">
                    <div class="container">
                        <legend>Find Schedule</legend>
                        <form>
                            <div class="row align-items-end">
                                <div class="col-md-3 col-sm-4">
                                    <label for="date">Desired Date</label>
                                    <br>
                                    <input type="date" id="date" value="<?//= $date ?>" required>
                                </div>
                                <div  class="col-md-3 col-sm-4">
                                    <label for="time">Desired Time</label>
                                    <br>
                                    <input type="time" id="time" value="<?//= $time?>" required>
                                </div>
                                <div  class="col-md-3 col-sm-4">
                                    <button class="btn btn-flat btn-primary" name="b1">Search</button>
                                </div>
                            </div>
                        </form>
                    </div>
            </fieldset>
        </div>
        <hr>
        <table>
            <colgroup>
                <col width="15%">
                <col width="15%">
                <col width="20%">
                <col width="20%">
                <col width="20%">
                <col width="10%">
            </colgroup>
            <thead>
                <tr class="bg-gradient-primary text-light">
                    <th>Train Number</th>
                    <th>Schedule</th>
                    <th>Train Name</th>
                    <th>Origin</th>
                    <th>Destination</th>
                    <th>class</th>
                    <th>Rate</th>
                    <th>Status</th>
                </tr>
            </thead>
            <!--php code to fetch data-->
                <?php
                global $con;
                $result=mysqli_query($con,"select * from train_schedule");

                while($row=mysqli_fetch_array($result))
                {
                ?>
                <tr>
                    <td><?php echo $row['train_no'];?></td>
                </tr>
                <tr>
                    <td><?php echo $row['date_schedule'];?></td>
                </tr>
                <tr>
                    <td><?php echo $row['train_name'];?></td>
                </tr>
                <tr>
                    <td><?php echo $row['origin'];?></td>
                </tr>
                <tr>
                    <td><?php echo $row['destination'];?></td>
                </tr>
                <tr>
                    <td><?php echo $row['class'];?></td>
                </tr>
                <tr>
                    <td><?php echo $row['price'];?></td>
                </tr>
                <tr>
                    <td><?php echo $row['status'];?></td>
                </tr>
                <?php 
                }
                ?>
                
        </table>

        </section>


    </body>

</html>

Additional suggestions on how to improve this code are also welcome because this I am a complete beginner at this.

This is the connection code:

<?php
            if(isset($_REQUEST['b1']))
                {
                    $con=mysqli_connect("localhost","root","","train");
                    if(!$con){
                    echo "not connect";
                    }
            }   
?>

I've tried several times and yes I have read through the other question already posted in here but the error still persists and I really have no idea on what to do here.

  • You need to use the error reporting functions to determine why `$conn` is `null`. – Quentin Nov 05 '22 at 19:41
  • @Quentin I did. It is indeed empty. – Virtualization Nov 05 '22 at 20:02
  • Well yes, we know that. Presumably you have some code that is supposed to assign it a value. And you need to use the mysqli_ error reporting functions to find out why that didn't work. – Quentin Nov 05 '22 at 20:13
  • 2
    Your `$con` variable seems to only ever get set `if(isset($_REQUEST['b1']))`. But your "scheduler" code doesn't reference that at all. Is there some other code that's ensuring that this template code is not run except in that case? – Greg Schmidt Nov 05 '22 at 22:44
  • @GregSchmidt I don't know but this is the entire code for the program. – Virtualization Nov 06 '22 at 15:06
  • If you don't know whether `$_REQUEST['b1']` is set when this code runs, then I don't know how to help you. If it's not set, then the code is definitely never initializing the connection, and that would be at least a big part of your problem. – Greg Schmidt Nov 06 '22 at 15:18

1 Answers1

-4

If I understand correctly, your connection variable appears to be null. You can use var_dump($con) to check.

if u have $con variable in your connection.php try global $con to $con.

rehdadth
  • 66
  • 1
  • 7