1

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>";
Shadow
  • 30,859
  • 10
  • 44
  • 56
  • 1
    at least make it good looking – Pathik Vejani Feb 27 '17 at 12:41
  • 2
    If you cannot be bothered to make your code sample readable. ___Why should we bother reading it___ You cannot debug code you cannot read – RiggsFolly Feb 27 '17 at 12:43
  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Feb 27 '17 at 12:46
  • 5
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Feb 27 '17 at 12:47
  • 1
    Replace `$row["site_title"]` with `$site_title` – Ravi Feb 27 '17 at 12:51

2 Answers2

1

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

Arun
  • 138
  • 10
  • Same problem occur after changing echo'value="'.$row["site_title"].'"'; into echo'value="'.$row_result["site_title"].'"'; – Ehtasham Ali Feb 27 '17 at 13:00
  • Just check what the exception says. Put the code inside "while" loop within a try -> catch block and print the exception. `try{ /* code within while loop goes here */ }catch (Exception $e) { echo $e->getMessage(); }` – Arun Feb 28 '17 at 04:53
0

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.

Gateway
  • 79
  • 7