Previous Page

How to output data retrieved from a database

Here is another, easier approach to retrieve the actual value of data stored in your database - IF it exists.


		
        //get the actual value of the user's name after they enter a username
		  $query = mysql_fetch_assoc(mysql_query(" SELECT `name` FROM `practice_table` WHERE `username` = '$username' "));						  			  
		
		  if($query['name'])// if there is a record with `username` = 'username' then assign it to a variable....or echo if you need to
		  {
			  $name = $query['name']; 
              // echo 'Hello, '.$query['name'];          	
		  }
          else
          {
          	  echo 'Sorry, that username does not exist!';
          }
          
          //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.';
//