Ehtasham Ali
Ehtasham Ali

Reputation: 73

echo statements are not working in while loop in php

I connect my code with database which is perfectly connected.But when I echo some data which is from the database after running the query the echo statement is not working.

<?php 

mysql_connect("localhost","root","");   
mysql_select_db("search");   
if(isset($_GET['search'])){
    $get_value = $_GET['user_query'];
    $result_query = "select * from sites where site_keywords= '%$get_value%' ";
    $run_results=mysql_query($result_query);        
    $site_val=$_GET['user_query'];
    echo "<div class='results'> $site_val </div>";       
    while($row_result=mysql_fetch_array($run_results)){

         $site_title=$row_result['site_title'];
         $site_link=$row_result['site_link'];
         $site_keyword=$row_result['site_keywords'];
         $site_desc=$row_result['site_desc'];
         $site_image=$row_result['site_image'];

        echo'value="'.$row["site_title"].'"';
        echo "<div class='results'> 
            <h2> $site_title </h2> 
            <a href='site_link' target='_blank'>$site_link</a>       
            <p align='justify'>$site_desc</p>        
            <img src='images/$site_image' width='100' height='100' />        
        </div>";
        }
    }
 ?>

This echo statement is working because this $site_val is not getting any data from the databse.

$run_results=mysql_query($result_query);

Below both echo statements are not working..

echo'value="'.$row["site_title"].'"';
echo "<div class='results'> 
<h2> $site_title </h2> 
<a href='site_link' target='_blank'>$site_link</a>
<p align='justify'>$site_desc</p>         
<img src='images/$site_image'
width='100' height='100' />          
</div>";

Upvotes: 1

Views: 1191

Answers (2)

Gateway
Gateway

Reputation: 79

Change

 echo'value="'.$row["site_title"].'"';

To

 echo'value="'.$row_result["site_title"].'"';

Or

echo'value=$site_title';

It's also advised that you use mysqli or PDO as mysql is now depreciated.

Upvotes: 0

Arun
Arun

Reputation: 136

You have used $row in the below code

echo'value="'.$row["site_title"].'"';
      echo "<div class='results'> 
      <h2> $site_title </h2> 
      <a href='site_link' target='_blank'>$site_link</a>
      <p align='justify'>$site_desc</p>
      <img src='images/$site_image'
width='100' height='100' />          </div>";

But you used $row_result to populate the data. use $row_result instead of $row

Upvotes: 1

Related Questions