Retrieving user information from database - help

Right, my code that I have written will retrieve the records from the database table containing the users
but I only want to display the details of the user logged in rather than all of the users.

I can't seem to work out how to change it so it only displays the logged in user.


$query = "SELECT * FROM user ";
$result = mysql_query($query);
$num = mysql_num_rows ($result);

if ($num > 0 ) {
while ($i < $num) {
$FirstName = mysql_result($result,$i,"FirstName");
$Surname = mysql_result($result,$i,"Surname");
$DOB = mysql_result($result,$i,"DOB");
$City = mysql_result($result,$i,"City");
$Postcode = mysql_result($result,$i,"Postcode");
$WebAddress = mysql_result($result,$i,"WebAddress");
$USER_ID = mysql_result($result,$i,"USER_ID");

echo "<b>First Name:</b> $FirstName<br>";
echo "<b>Surname:</b> $Surname<br>";
echo "<b>Date of Birth:</b> $DOB<br>";
echo "<b>City:</b> $City<br>";
echo "<b>Postcode:</b> $Postcode<br>";
echo "<b>Website:</b> $WebAddress<br>";
echo "<a href=\"account_save.php?id=$USER_ID\">Update</a>";
echo "<br><br>";

++$i; } } else { echo "The database is empty"; }


Open in new window

Your help is much appreciated.
Thank you.
Who is Participating?
maeltarConnect With a Mentor Commented:
Why not use a while loop to run though and put it within an if statement :

Obviously the $row array elements need to be adjusted to suit.
if ( $num > 0)
while ($row = mysql_fetch_array($result)
echo "<b>First Name:</b> {$row[0]}<br>";
echo "<b>Surname:</b> {$row[1]}<br>";
echo "<b>Date of Birth:</b>{$row[2]}<br>";
echo "<b>City:</b> {$row[3]}<br>";
echo "<b>Postcode:</b> {$row[4]}<br>";
echo "<b>Website:</b>{$row[5]}<br>";
echo "<a href=\"account_save.php?id=$id\">Update</a>";
echo "<br><br>";


} // end if

Open in new window

NOTE  Line 3 should read
while ( $row = mysql_fetch_array($result) )

Open in new window

PSTCATAuthor Commented:

Thanks for your reply,

That code works for displaying the user's information
but I am still getting the other users in the table being displayed on the page.

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

how do you know what user is logged in ?
As in is there a variable set to a value ?
maeltarConnect With a Mentor Commented:
If the user id is set in a variable you could adjust the sql to something similar to :

$query = "SELECT * FROM user where <user id field> = <variable holding user id>";

Open in new window

PSTCATAuthor Commented:
I see where you are going with that.

Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.