Solved

Retrieving user information from database - help

Posted on 2011-09-06
6
230 Views
Last Modified: 2012-05-12
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.

<?php
include("database.php");

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

if ($num > 0 ) {
$i=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.
0
Comment
Question by:PSTCAT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
maeltar earned 500 total points
ID: 36489899
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

0
 
LVL 11

Expert Comment

by:maeltar
ID: 36489911
NOTE  Line 3 should read
while ( $row = mysql_fetch_array($result) )

Open in new window

0
 

Author Comment

by:PSTCAT
ID: 36489963
Hi,

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.

0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 11

Expert Comment

by:maeltar
ID: 36490051
how do you know what user is logged in ?
As in is there a variable set to a value ?
0
 
LVL 11

Assisted Solution

by:maeltar
maeltar earned 500 total points
ID: 36490065
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

0
 

Author Comment

by:PSTCAT
ID: 36490087
I see where you are going with that.

Thank you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question