0

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • ___To die or not to die___ This would make a good read for you as well https://stackoverflow.com/questions/15318368/mysqli-or-die-does-it-have-to-die – RiggsFolly Apr 14 '22 at 15:47
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Apr 14 '22 at 15:48
  • You have not put the database name in parameter 4 of the `$con = mysqli_connect($server, $username, $password);` So no database is being used. Take a few minutes to look at the [PHP Manual pages for the MySQLI Extension](https://www.php.net/manual/en/mysqli.construct.php) – RiggsFolly Apr 14 '22 at 15:51
  • You only create the database connection if the form has submitted. The rest of the code has no such restriction. – aynber Apr 14 '22 at 15:55
  • Your connection script is very wrong. Do not check `if(isset($_POST['name'])){` in there, that will only actually attempt to make a connection if that `$POST` variable actually exists and I guess it does not – RiggsFolly Apr 14 '22 at 15:56
  • @RiggsFolly I also saw that problem but the connection works when it is used first, right? So we know that’s not the problem… – Archit Gargi Apr 14 '22 at 15:57
  • @ArchitGargi Regardless, there is no reason or sence in putting that or any other test in the connection scriptlet – RiggsFolly Apr 14 '22 at 15:58
  • @RiggsFolly yes u r right but I was just pointing out an mistake in ur previous comment – Archit Gargi Apr 14 '22 at 16:00
  • @ArchitGargi I would hazzard a polite guess that the code that works is not using the same connection script. Specially as there is no database mentioned in the `mysqli_connect()` so no sql would work – RiggsFolly Apr 14 '22 at 16:00
  • The no database selected isn't an issue *yet* because your connection isn't being created, but it will be once you get that resolved. – aynber Apr 14 '22 at 16:01
  • @RiggsFolly yes maybe u r right… And if it is then the answer to the question will not work – Archit Gargi Apr 14 '22 at 16:02

0 Answers0