I have a form where I am saving images and other details in the Db successfully, but when it comes to fetching the data from the database I am getting an error.
Here is how i am connecting to data base":
<?php
if(isset($_POST['name'])){
    $server="localhost";
    $username="root";
    $password="";
    $con = mysqli_connect($server, $username, $password);
    if(!$con) {
        die("Connection failed" .
        mysql_connect_error());
    }
}
?>    
Now I am facing no issues in storing the data but when I try to fetch, I get the error:
Warning: Undefined variable $con in C:\xampp\htdocs\test\view.php on line 7
Warning: Undefined variable $con in C:\xampp\htdocs\test\view.php on line 8
Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\test\view.php:8 Stack trace: #0 {main} thrown in C:\xampp\htdocs\test\view.php on line 8
Below is my file where I am fetching and getting the above error:
<?php 
  
require_once 'db_connect.php';// calling my file where i am connecting to db
$result= $con->query("SELECT name FROM form ORDER BY s.no DESC");   //HERE CON IS COMING 
UNDEFINED
?>
<?php if($result->num_rows > 0){ ?> 
<div class="gallery"> 
    <?php while($row = $result->fetch_assoc()){ ?> 
        <img src="data:image/jpg;charset=utf8;base64,<?php echo base64_encode($row['images']); 
  ?>" /> 
    <?php } ?> 
</div> 
<?php }else{ ?> 
<p class="status error">Image(s) not found...</p> 
<?php } 
$con->close(); 
?>
Please help I am stuck on it for days and still clueless as I am new in PHP
 
    