Jump to content

echo php in between html code


nadiam
Go to solution Solved by Ch0cu3r,

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
Share on other sites

  • Solution

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.

Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.