Previous Page

How to output data retrieved from a database

You can insert the following code inside the curly brackets of the IF statement if ($num_rows > 0) in your mysql_login.php right before the confirmation of successful login.

NOTE: If you ever get an error about the mysql_fetch_array, try to remove the single quotes around the variables in the MySQL statements (not the table name or fields, just the variables.) I've had this problem before and removing them worked. I believe it had something to do with a function I use to defend from SQL injection, as I will show you how to do soon.


		//get name from database after the user's credentials are successful
		  $name = (" SELECT `name` FROM `practice_table` WHERE `username` = '$username' ");
		
		  $result = mysql_query($name); // results of your query
				  			  
		//get the actual value of the user's name
		  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
		  {
              $name =  $row['name'];
          }
          
          //create a session variable to pass to other pages for the user's name
		    $_SESSION['name'] = $name;
            
// END CODE
          

This way you use the session variable in other pages like below.

//
      echo 'Welcome back '.$_SESSION['name'].', You have successfully logged in.'; 
//