Log in

View Full Version : Viewing User Profiles



smithster
05-13-2008, 02:43 PM
Ok, a little hard to explain...

This is usually found in a forum or a cms. Users have an id number 1,2,3... and forum threads also have an id number. I'm hoping to expand my PHP knowledge a little further! Currently to view someones profile, I have all users listed on a page along with a submit button next to each name which carries a hidden reference to the data in mysql. The submit button's hidden variable is stored as a session variable and passed to a profile.php page which queries the database to retrieve the data for that user.

Often, when viewing someone's profile say here on Tizag the website address is something like...

http://forums.tizag.com/member.php?u=6914

So my question is how do I get my setup to work like that?

Here's a quick script I put together to work on...



<?php

mysql_connect(localhost,user,password);
mysql_select_db(database);

$query = "SELECT * FROM users";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)){
echo '<a href="profile.php?u='.$row['id'].'">'.$row['firstname'].'</a>';
echo '<br />';
}
?>


Assuming the above is the correct way to go about this, I still have no clue where to even start with the profile.php page!! If anyone can help that would be great! :D

Thanks in advance.

thetestingsite
05-13-2008, 05:44 PM
It would be something along the lines of the following:



<?php
mysql_connect(localhost,user,password);
mysql_select_db(database);

if (isset($_GET['u']) && $_GET['u'] != '') {
$query = "SELECT * FROM users WHERE `id`=".$_GET['u'];

$result = mysql_query($query);

if (!mysql_num_rows($result)) {
die('User Not Found');
}

else {
echo '<pre>';
print_r(mysql_fetch_array($result));
echo '</pre>';
}
}

else {
die('You must enter a user id before continuing');
}
?>


Hope this helps.