How to Retrieve an image with details on another page

hi i am trying to Retrieve an image with details on another page. using the id but noting displays and i get the id when i var_dump(); the getRows(); returns null i am stock here would need help. here is my code. thanks.

if (!isset($_GET['pro_id'])){

}

else{

	$pro_id=$_GET['pro_id'];

//select from the db

 $query= "SELECT * FROM `property` where pro_id=:pro_id limit 1";

$params=[':pro_id'=>$pro_id];
  var_dump($params);

$property=$db->getRows($query,$params);
var_dump($property);
if (!$property){
 $result[]='Empty array';
}else{
  $property=[0];
  $propertyname=$property['property_name'];
  $property_location=$property['property_location'];
  $propertyprice=$property['property_price'];
  $category_id=$property['category_id'];
  $status=$property['status'];

   $query= "SELECT * FROM `property_img` where pro_id=:pro_id limit 1";
  $params=[':pro_id'=>$id];

  $property_img=$db->getRows($query,$params);
var_dump($property_img);

$property_img=[0];
$property_img=$property_img['property_img'];
}

?>
<!DOCTYPE html>
<html>
<head>
	<title>Property</title>
</head>
<body>

<?php 

if (isset($result)){
  implode( " " , $result);
  exit;
}
?>
    
      	<div class="container">

  <!-- Full-width images with number text -->
  <div class="row">
  <div class="property_name col-md-8 col-sm-6 col-lg-8">
  	<h1><?php echo  $propertyname; ?></h1> <div class='image'>
  	<div class="mySlides">
      <div class="numbertext">1 / 6</div>

         <?php     

          foreach ($property_img as $property_img) {
                    $pro_id = $property_img['pro_id'];
                    $property_img = $property_img['property_img'];
                    ?>
                    <tr>
                        <td>
                            <img src="<?php echo 'UploadFolder/'.$property_img; ?>" alt="" />
                        </td>
                    </tr>
                    <?php
                }
                ?>
      
  <div class="description">
  	<?php  echo $property_location;?>
  	</div>
  </div>

     <div class="column">
      <ul><li>
        <img class="demo cursor" src="<?php
  echo  $property_img; 

   //echo $img ?>" style="width:100%" onclick="currentSlide(1)" alt="" /></li></ul>
      </div>

  </div>
</div>
  <div class=" col-md-3 ">
  	
  	<hr><h2>Agent</h2>
       <img src="image/gmplogo.jpg" width="150px" height="120px"><br>
      <!--soial media links---->
       <div class="social-links">
                          	<ul class="social-icon-four">
                              	 <li><a href="#"><span class="fa fa-facebook"></span></a></li>
                                <li><a href="#"><span class="fa fa-twitter"></span></a></li>
                                <li><a href="#"><span class="fa fa-whatsapp"></span></a></li>
                              </ul>
                          </div>
       <button><a href="" class="btn_info">Contact agent</a></button>
       <button><a href=""class="btn_info">Agent information</a></button>
  	<hr><h2> Description</h2>
        <p> <?php echo  $property_location;?>  </p>

  	<hr><h2> Property Details</h2>

  	<p>     Property: <?php echo$propertyname;?></p>
  	<p>Property Location: <?php echo  $property_location;?></p>
  	<p>Property Price: <?php echo $propertyprice;?></p>
  	<p> Agent: MR Felix </p>
  	
</div>

</div>

<div class="row">
  <div class=" col-md-3"> <hr><h2>Agent</h2>
       <img src="image/gmplogo.jpg" width="150px" height="120px"><br>
      <!--soial media links---->
       <div class="social-links">
                            <ul class="social-icon-four">
                                <li><a href="#"><span class="fa fa-facebook"></span></a></li>
                                <li><a href="#"><span class="fa fa-twitter"></span></a></li>
                                <li><a href="#"><span class="fa fa-whatsapp"></span></a></li>
                            </ul>
                          </div>
       <button><a href="" class="btn-info">Contact agent</a></button><br>
       <button><a href=""class="btn-info">Agent information</a></button><br>
     </div>
     <div class="col-md-6">  
    <hr><h2> Description</h2>
        <p> <?php echo $property['property_location'];?>  </p>
      </div>
      <div class=" col-md-3">

    <hr><h2> Property Details</h2>

    <p>     Property: <?php echo $property['property_name'];?></p>
    <p>Property Location: <?php echo $property['property_location'];?></p>
    <p>Property Price: <?php echo $property['property_price'];?></p>
    <p> Agent: MR Felix </p>

here is my function.

public function getRows($query,$params=[]){
				try{
				$stmt=$this->datab->prepare($query);
				$y=$stmt->execute($params);
				$x=$stmt->fetchAll();
				return $x;
				echo $x;
				echo $y;
				}
				catch (PDOException $e){
				throw new Exception($e->getMessage());
				}
			}
1 Like

Right. Well. Lets start with some basic debugging, because… wow that was a lot.

the echo statements will never fire, because return stops execution of the function. Move them above the return line if you want them to actually spit something out.

Standard Emergency Procedure #1: Start putting echoes, var_dumps, and other debug commands everywhere until you figure out where the problem is.

In this case, i’d start with print_r($stmt->debugDumpParams()); just before your return line.

That will at least make sure your function got the right SQL query and the right parameters.
Also, for the sake of others that come after, this page is (or at least should be) set to throw Exceptions for all database errors.

Have you actually made sure your database does have the correct information matching to your query?

congratulations as you are awarded Member of the Month. thanks
this line of code print_r($stmt->debugDumpParams()); gave me these
ā€œSQL: [53] SELECT * FROM property where pro_id=:pro_id limit 1 Params: 1 Key: Name: [7] :pro_id paramno=-1 name=[7] ā€œ:pro_idā€ is_param=1 param_type=2ā€.

And you have a entry in your property table with an integer pro_id set to -1?

EDIT: Er nope, thats not what that param data reads, Marc. Try again.

What is in the parameter value? Echo it out.

no i believe the issue should be that i am giving only one param where i have six parameters.

There is exactly 1 parameter in the query.

Where does $id come from here? Obviously that only affects the image, not the first query.

On it displaying nothing, there’s a part of this code that might be causing it:

  $property=[0];
  $propertyname=$property['property_name'];
  $property_location=$property['property_location'];
  $propertyprice=$property['property_price'];
  $category_id=$property['category_id'];
  $status=$property['status'];

i made a private repository where you can have a close look at my code. here is the address https://github.com/Codehouse19/realestate gold.zip is the db. The page i am currently working on is property.php to get there you click any property in the index.php . thanks.

these answers #5
And you have a entry in your property table with an integer pro_id set to -1?

Dumps the information contained by a prepared statement directly on the output. It will provide the SQL query in use, the number of parameters used ( Params ), the list of parameters with their key name or position, their name, their position in the query (if this is supported by the PDO driver, otherwise, it will be -1)

I’d rather look at the code on here, and I really don’t want to download a load of stuff to my own machine.

Have you fixed the two things I mentioned in my post above?

yes i have done that am having same issues. but i think the issues is with the fetchAll in the database it’s returning null. i try to vardump() the execute its returning true.

OK, so please post the code as it currently is.

i use this code to retrieve the array from the database. what do you think?

my function

public function getRows($query,$params=[]){
				try{
				$stmt=$this->datab->prepare($query);
				$x=$stmt->execute($params);
				var_dump($x);
				//print_r($stmt->debugDumpParams());
				return  $stmt->fetchAll(); 

Look at that code (not the function, post #14) carefully, you’ll see there’s a problem with it, as I mentioned earlier. Look at each line in turn. Echo some values out. See what’s happening.

here is the logic i get my id using Get.
pass it through a query to get an array using getRows.
assign the result to these variables.

if (!isset($_GET['pro_id'])){

}

else{

	$pro_id=$_GET['pro_id'];
//select from the db

 $query= "SELECT * FROM `property` where pro_id=:pro_id limit 1";

$params=[':pro_id'=>$pro_id];
  var_dump($params);

$property=$db->getRows($query,$params);
var_dump($property);
if (!$property){
 $result[]='Empty array';
}else{
  $property=[0];
  $propertyname=$property['property_name'];
  $property_location=$property['property_location'];
  $propertyprice=$property['property_price'];
  $category_id=$property['category_id'];
  $status=$property['status']; 

Look at the lines of code that I highlighted in post number 8 earlier. Look at them one-by-one, turn on error reporting, and see what’s going wrong. You’re losing focus. There is an error in those lines of code.

There is a line of code that does not belong, and it’s causing the problem. What does each of these lines of code do? Add a comment to each one to say what it does.

  $property=[0];
  $propertyname=$property['property_name'];
  $property_location=$property['property_location'];
  $propertyprice=$property['property_price'];
  $category_id=$property['category_id'];
  $status=$property['status'];

Please mention exact solution

Thanks
Nikhil

No, sorry, I’m not going to do that. I want to try to help the OP learn how to spot problems and fix them so that next time, they can do it, and I’m sorry if that sounds unhelpful or patronising.

I’ve narrowed it down to a single block of code that I believe is causing the main issue (and it’ll be embarrassing if it is not!), and if they look closely at each line individually, I’m sure they will spot it.

3 Likes