1

I am currently working on a member login script that uses PHP, Mysql, and Jquery (What I need help with only pertains to the PHP and Mysql database though). I am using a tutorial/script for this: Tutorial

So my scripting at the moment looks similar to the coding in the tutorial, with changes for my specific Mysql database. What I am currently trying to achieve is that once a user is logged in, I want specific information to be shown to them, such as their name, rank, etc. I have rows in the mysql database for this that also houses the ID, username, etc. Specifically, there is a spot that I want it to show in the script, which is shown in the "demo.php" part:

<?php
     // FROM TUTORIAL PAGE 
     if(!$_SESSION['id']):
?> 
[...]
<?php
else:

?>

<div class="left">

    <h1>Members panel</h1>

        <p>You can put member-only data here.
</p>
        <a href="registered.php">View a special member page</a>
        <p>- or -</p>
        <a href="?logoff">Log off</a>

        </div>

        <div class="left right">
        </div>

<?php
    endif;
?>

Where it says "you can put member-only data here", I want the code to pull from the database to show the information I want specific for the username logged in. For example, if user "dan124" is logged in, where member-only data is, I want it to show dan124's current rank, points accumulated, stuff like that. An example is one row that I want pulled from the database is "pts".

I have tried several different codes and tinkered with some stuff I have found, but I cannot get what I'm trying to achieve. I know my way around basic database pulls, but this is my first time working specifically with logged in user based info.

If anyone can help with this, I'd really appreciate it. I'm sure I'm not the first person who has used the tutorial I'm working with and wanted this kind of information pull, I just can't find anything! If you need more info, let me know.

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
Cloudy
  • 37
  • 1
  • 2
  • 10
  • `else`. What is the `if`? You want to display this only if someone is logged in, how do you check for that? How do you store the session state? – Thom Wiggers Dec 05 '11 at 00:16
  • The if is shown in the full coding in the tutorial link that I provided at the top, since I'm working with the coding from that tutorial. The if is in Step 2 and starts on line 51 I believe – Cloudy Dec 05 '11 at 00:27
  • I had already submitted my edit - which I believe has been peer-reviewed now. – Thom Wiggers Dec 05 '11 at 00:28
  • What have you tried? What hasn't worked? It's a lot easier for people to help if you give this info :) – Michael Mior Dec 05 '11 at 00:37

2 Answers2

1

I may be misunderstanding what you are asking, but I am hearing that you need help with the basic database architecture and SQL.

A very common way to use MySQL would be to have a visitor table that looks like this:

visitor_id|visitor_login_name|visitor_current_rank|visitor_points_accumulated
----------|------------------|--------------------|---------------------------
1         |dan124            |veteran             |1254
6892      |mary8             |honored guest       |1

Now, for the life of me I don't know how you are getting the visitor logged in without doing more complicated things than this already, but assuming you have them logged in with their login_name available as a session variable, you would use the following query to get the info you want.

$query = "SELECT * FROM `visitor` WHERE `visitor_login_name` = $_SESSION[visitor_login_name]";

Then you will run that query, and the results will have the information you need as members of the result array, like this:

$rank = $result['visitor_current_rank'];

If you can give me some feedback as to how well I have guessed your intent, we can move a little further from here.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Tom Haws
  • 1,322
  • 1
  • 11
  • 23
  • You guessed my intent pretty well. Currently my MYSQL table has the current fields: `id, usr, pts, rank, pass, email, regIP, dt`. The primary key is id and the unique key is usr. pts is for points, rank is for rank, etc. I'm wanting the information to show up on line 86 as shown in step 2 on the tutorial, where the coding has already logged the session in. – Cloudy Dec 05 '11 at 01:14
  • Oh, also, I have been assuming that part of the php code would be something like `echo "Points:" .$row['pts']. "";` like I use in my non-user database pulls, but I have been unable to get a line like that to work for this. – Cloudy Dec 05 '11 at 01:23
  • Managed to work my coding out using some of the variables you provided. I posted the actual code as my answer, thanks so much Tom! – Cloudy Dec 08 '11 at 19:56
1

Ok so much thanks to Tom Haws whose answer helped me figure out the coding I needed with some of the coding he provided. After a lot of trial and error, I managed to work out the php coding to pull specific information out of a persons database and show it when they're logged in. This is the coding I got it to work with:

<?php
if($_SESSION['id'])
$result = mysql_query("SELECT * FROM database WHERE `id` = $_SESSION[id]")
    or die(mysql_error());  

     while($row = mysql_fetch_array( $result )) {
 echo '<b>Points Accumulated:</b>' .$row['pts'];
 echo '<br>';
 echo '<b>Rank:</b>' .$row['rank'];
 }?>

This resulted in the logged in user being shown ONLY HIS/HER points and rank, which is what I was trying to achieve.

Cloudy
  • 37
  • 1
  • 2
  • 10