Jump to content

Accessing user specific Information from a MYSQL Database


wdlyons

Recommended Posts

Hi,

 

I have created a session based logon system using php and MYSQL from some tutorials I found online which is working very successfully. I can log on and of and move through different pages with no problems.

 

My query is how do I output or display the information that is specific to the user which is currently logged on and block access to any other users information. I am quite sure there is a simple solution that is escaping me.

 

If you could point me in the right direction it would be greatly appreciated.

 

Thanks in advance

Link to comment
Share on other sites

What exactly do you mean by infromation specific to the user?

Depending on how you track a users online status you can query your users table asking for that users information:

$query = mysql_query("select username from tables where id = '{$_COOKIE['user_id']}'");
$result = mysql_fetch_assoc( $query );
echo 'Hello ' . $result['username'];

 

Of course if you don't use cookies you'll need to either check $_SESSION or your mysql table that tracks user sessions. That part depends on how your code is set up.

Link to comment
Share on other sites

Hi,

 

Sorry I should have put the code:

 

First File: auth.php

 

<?php

//Start session

session_start();

 

//Check whether the session variable SESS_MEMBER_ID is present or not

if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {

header("location: access-denied.php");

exit();

}

?>

 

 

Second File: Profile.php

 

<?php

require_once('auth.php');

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>My Profile</title>

<link href="loginmodule.css" rel="stylesheet" type="text/css" />

</head>

<body>

<h1>My Profile </h1>

<a href="member-index.php">Home</a> | <a href="logout.php">Logout</a>

<?php

//Include database connection details

require_once('config.php');

 

//Connect to mysql server

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$link) {

die('Failed to connect to server: ' . mysql_error());

}

 

//Select database

$db = mysql_select_db(DB_DATABASE);

if(!$db) {

die("Unable to select database");

}

 

 

 

// Collects data from "members" table

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

or die(mysql_error());

 

  // puts the "friends" info into the $info array

$info = mysql_fetch_array( $data );

 

  // Print out the contents of the entry

  Print "<b>Member ID:</b> ".$info['member_id'] . " ";

Print "<b>First Name:</b> ".$info['firstname'] . " ";

Print "<b>Last Name:</b> ".$info['lastname'] . " <br><br>";

 

Print "</table>";

?>

</body>

</html>

 

I will try and explain a little better too!!

 

When a user logs in and selects the link for their profile they only see the first row in the table not their own details.

 

I am seeking guidance on how I would go about showing the logged on user (who has the session started) details only ie by somehow using the users member_id or login id as a way to fetch the information out of the table.

 

I hope this makes sense.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.