Swift Learner
Swift Learner

Reputation: 83

Get the data of specific user in PHP

Now I have created a login form with a session, what I need now that when the user login with his username and password, get his data such as name, about etc.. and put it in the welcome page.

Currently I have created this code but this code get all users data,

 <?php 
 mysql_connect("localhost", "root", "") or die(mysql_error()); 
 mysql_select_db("usersdata") or die(mysql_error()); 
 $data = mysql_query("SELECT * FROM userid") 
 or die(mysql_error()); 
 Print "<table border cellpadding=3>"; 
 while($info = mysql_fetch_array( $data )) 
 { 
 Print "<tr>"; 
 Print "<th>Name:</th> <td>".$info['Name'] . "</td> "; 
 Print "<th>Username:</th> <td>".$info['Email'] . " </td></tr>"; 
 } 
 Print "</table>"; 
 ?> 

I hope to find a way to do that. :D

Upvotes: 3

Views: 63752

Answers (4)

Michael
Michael

Reputation: 1231

Your example code retrieves all users from the database and loops through the data using a while loop.

To get the user that has logged in you need to change your query that fetches the data. I'm assuming you have a primary key in your table and know the id because the user already logged in.

$data = mysql_query("SELECT * FROM userid WHERE id={$userid}");
$info = mysql_fetch_array( $data );
echo $info['Name'];

$info will now contain all the user info for 1 user, you need to fill $userid with the actual id from the user that is logged in.

Upvotes: 0

Omar Masad
Omar Masad

Reputation: 155

Since you already created a login form with session then you get the data for the current logged in user by doing this:

$_SESSION['userid']: Should be filled in the login page. $_SESSION['userid'] = $id

Learn more about the sessions: PHP Sessions W3schools

And then:

$query= mysql_query("SELECT * FROM `userid` WHERE `id` = '".$_SESSION['userid']."' ")or die(mysql_error());
$arr = mysql_fetch_array($query);
$num = mysql_numrows($query); //this will count the rows (if exists) 

HTML

<html>
 //...
<?php if($num > 0){ ?>
<table border="1" cellpadding="3">
<tr><td colspan="2" align="center">Your Info</td></tr>
<tr>
 <td>Name: <?php echo $arr['Name']; ?></td>
</tr>

<tr>
 <td>Email: <?php echo $arr['Email']; ?></td>
</tr>
</table>
<?php }else{ ?>
 User not found.
<?php } ?>
 //...
</html>

Upvotes: 1

amaster
amaster

Reputation: 2163

$data = mysql_query("SELECT * FROM userid")

Should be

$data = mysql_query("SELECT * FROM userid WHERE Name='$selectedName'")

Of course you need to define $selectedName

I also recommend you read http://dev.mysql.com/doc/refman/5.0/en/select.html to learn about some fundamentals.

Upvotes: 0

Jacob S
Jacob S

Reputation: 1701

Although you should use the mysqli_ extension, rather than mysql_, you would want something like:

$result = mysql_query("SELECT * FROM userid WHERE username = '" . $username . "'")
    or die(mysql_error());
if(mysql_num_rows($result) == 1) {
    //Found the user
    $row = mysql_fetch_array($result);
    //Results can be accessed like $row['username'] and $row['Email']
} else {
    //Too few or too many records were found
}

Note: I've used username='$username' as an example. It would be best to track the user's ID from the login process as the ID refers to a specific row.

Upvotes: 1

Related Questions