Jump to content

echo php in between html code


nadiam

Recommended Posts

this is my php code for my profile.php. it is to display the users name after login.

 

 

  1. <?php
  2. session_start();
  3.  
  4. require "connect.php";
  5. if($_SESSION['name'])
  6. {
  7. $name = $_SESSION['name'];
  8. $query = mysql_query("SELECT name FROM register WHERE name ='$name'");
  9. $numrows = mysql_num_rows($query);
  10.  
  11. if(1 == $numrows)
  12. {
  13. while ($rows = mysql_fetch_assoc($query))
  14. {
  15. echo "Welcome, ".$rows['name']."!";
  16. }
  17. }
  18. }
  19.  
  20. ?>

 

i want to display this:

 

 

  1. while ($rows = mysql_fetch_assoc($query))
  2. {
  3. echo "Welcome, ".$rows['name']."!";
  4. }

 

between some html. ive tried but it isnt working. this is the code i did:

 

<?php if(1 == $numrows) { ?><?php while (@$rows = mysql_fetch_assoc(@$query))?><h3><?php echo "Welcome, ".@$rows['name']; ?><?php }?>

 

i tried:

 

<?php while (@$rows = mysql_fetch_assoc(@$query))?><h3><?php echo "Welcome, ".@$rows['name']; ?>

 

but both doesnt work. as in only "Welcome, " is displayed in my page.

 

any pointers is much appreciated! :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/283992-echo-php-in-between-html-code/
Share on other sites

You already have the name in $_SESSION['name'] variable so why are you querying the database for their name again?

 

You can just use this.

echo "Welcome, ".$_SESSION['name']."!";

Also note you only use the while loop if your query returns more than one row.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.