PHP How to select and display a row from mysql table after user login

So I am creating a site where people have to register and login, and I was wondering how to display user information after login?

The code for my site page (members.php) looks like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

session_start(); // the session variable in the login form is  $_SESSION['emailaddress']




 echo "your email is... ".$_SESSION['emailaddress']; // i used only one session variable


 $result = mysql_query("SELECT * FROM userinfo WHERE email='$user'");

while($row = mysql_fetch_array($result))
  echo $row['firstname'] . " " . $row['lastname'] . " " . $row['password'];   




However, when I go to the members page after logging in, it only displays the user's email address in the first echo (echo "your email address ...". $ _ SESSION ['emailaddress'];). Is there something wrong with the algorithm above used to select user data in a row?


source to share

2 answers

The best workaround is to first print the request in the browser

$query = "SELECT * FROM userinfo WHERE email='$user'";
echo $query; 
$result = mysql_query($query);

$row = mysql_fetch_array($result);
echo $row['firstname'] . " " . $row['lastname'] . " " . $row['password'];    



Btw, you don't need a while clause loop when fetching 1 line.

Once you get the query, copy it and run it directly on the database and check the results using MySQL GUI or MySQL Workbench tools, I'm sure you will find the problem;

  • Hello


On line 14, during the execution of your query, you forgot to enable the database connection in $result

. See below.


$result = mysql_query("SELECT * FROM userinfo WHERE email='$user'",$con);




All Articles