2

I'm trying to echo a variable from my php script inside a div but i'm not getting any result.Here is my code :

<?php
session_start();
session_name();

$con = mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("bloggers", $con) or die(mysql_error()); 


    $data = "Select * from accountprofile where Username = '" . $_SESSION['Sess_user'] . "' and Password = '" . $_SESSION['Sess_pass'] . "'";

$result = mysql_query($data);

while($row = mysql_fetch_array($result)) {
   $row['Fname'];
   $row['Username'];
   $row['Password'];

  }
  mysql_close($con);

  ?>

and here is my div where i want to display the variable $row['Fname'];

<div class="title" style="font-size: 36px;">
    <?php echo $row['Fname']; ?>
</div>

Sorry i am just new in html and php .Please help thanks.

query
  • 531
  • 3
  • 7
  • 30
user2047260
  • 21
  • 1
  • 2

4 Answers4

2

You're close. Here is your code simplified and corrected:

<?php
session_start();
session_name();

$con = mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("bloggers", $con) or die(mysql_error()); 

$data = "Select * from accountprofile where Username = '" . $_SESSION['Sess_user'] . "' and Password = '" . $_SESSION['Sess_pass'] . "'";

$result = mysql_query($data);
$row = mysql_fetch_assoc($result); // You're only getting one row, no need to loop.
mysql_close($con);

 ?>

<div class="title" style="font-size: 36px;">
    <?php echo $row['Fname']; ?>
</div>

FYI: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
2

The variable $row is only loop-scope. You're creating this variable inside while and it will only exist there.

Marco Aurélio Deleu
  • 4,279
  • 4
  • 35
  • 63
0

@user2047260 remember the variable you whant to display it has to be inside the while loop bellow is the code try it

 <?php
    session_start();
    session_name();

    $con = mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("bloggers", $con) or die(mysql_error()); 


        $data = "Select * from accountprofile where Username = '" . $_SESSION['Sess_user'] . "' and Password = '" . $_SESSION['Sess_pass'] . "'";

    $result = mysql_query($data);

    while($row = mysql_fetch_array($result)) {
       $fname = $row['Fname'];
       $username = $row['Username'];
       $password =$row['Password'];
      ?>
      <div class="title" style="font-size: 36px;">
        <?php echo $fname;
          mysql_close($con);
        }
        ?>
    </div>



        enter code here
Humphrey
  • 2,659
  • 3
  • 28
  • 38
-1

If you are echoing a variable inside an echo statement, you can do:

echo "foo " . $bar . "!";

This is called concatenating a string.

If you are looking to use it later on, you can put all of the results into an array by using the following in your while loop

while($row = mysql_fetch_array($result)) {
   $useLaterOn[] = $row;
  }

Hopefully this helps :)