nadiam Posted November 17, 2013 Share Posted November 17, 2013 this is my php code for my profile.php. it is to display the users name after login. <?php session_start(); require "connect.php"; if($_SESSION['name']) { $name = $_SESSION['name']; $query = mysql_query("SELECT name FROM register WHERE name ='$name'"); $numrows = mysql_num_rows($query); if(1 == $numrows) { while ($rows = mysql_fetch_assoc($query)) { echo "Welcome, ".$rows['name']."!"; } } } ?> i want to display this: while ($rows = mysql_fetch_assoc($query)) { echo "Welcome, ".$rows['name']."!"; } 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! Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted November 17, 2013 Solution Share Posted November 17, 2013 (edited) 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 November 17, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
nadiam Posted November 17, 2013 Author Share Posted November 17, 2013 Thanks!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.