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']
?>


<html>
<head>

</head>
<body>

<?php

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

 $user=$_SESSION['emailaddress'];


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

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

 ?>


</body>
</html>

      

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?

+3


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
+1


source


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

. See below.



$con=mysql_connect(hostname,username,password,databasename);

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

      

0


source







All Articles