Jump to content

Show data for logged in user


scoobybrew

Recommended Posts

I want to show data for logged in user, i am using sessions to login. This is the code i already have:

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");



//this selects everything for the current user, ready to be used in the script below

$result = mysql_query("SELECT id, points, ingame_points, ingame_money, ingame_items FROM members;
WHERE username = $_SESSION['myusername']");

//this function will take the above query and create an array
while($row = mysql_fetch_array($result))
  {

//with the array created above, I can create variables (left) with the outputted array (right)
$points = $row['points'];
$id = $row['id'];
$ingame_points = $row['ingame_points'];
$ingame_money = $row['ingame_money'];
$ingame_items = $row['ingame_items'];
  }

 

 

Help  :-\?

Link to comment
https://forums.phpfreaks.com/topic/226489-show-data-for-logged-in-user/
Share on other sites

<?php
  // Connect to server and select database.
  mysql_connect("$host", "$username", "$password")or die("cannot connect");
  mysql_select_db("$db_name")or die("cannot select DB");

  //this selects everything for the current user, ready to be used in the script below

  $query = "SELECT id, points, ingame_points, ingame_money, ingame_items FROM members WHERE username='{$_SESSION['myusername']}' LIMIT 1";

  if($doQuery = mysql_query($query)) {
    if(mysql_num_rows($doQuery)) {
      $user = mysql_fetch_assoc($doQuery);

      print_r($user);

    } else {
      echo 'No result returned for the query: '.$query;
    }

  } else {
    echo 'The following query failed: '.$query;
  }

 

Try the above code, tell me if it is what you're looking for :)

 

Regards, PaulRyan.

  Quote

<?php
  // Connect to server and select database.
  mysql_connect("$host", "$username", "$password")or die("cannot connect");
  mysql_select_db("$db_name")or die("cannot select DB");

  //this selects everything for the current user, ready to be used in the script below

  $query = "SELECT id, points, ingame_points, ingame_money, ingame_items FROM members WHERE username='{$_SESSION['myusername']}' LIMIT 1";

  if($doQuery = mysql_query($query)) {
    if(mysql_num_rows($doQuery)) {
      $user = mysql_fetch_assoc($doQuery);

      print_r($user);

    } else {
      echo 'No result returned for the query: '.$query;
    }

  } else {
    echo 'The following query failed: '.$query;
  }

 

Try the above code, tell me if it is what you're looking for :)

 

Regards, PaulRyan.

 

Thanks, but that doesnt work..

it says

  Quote
No result returned for the query: SELECT id, points, ingame_points, ingame_money, ingame_items FROM members WHERE username='' LIMIT 1

Whats wrong?

  Quote

I don't know if that's all the code in case it is, you need to start the session for that page if not the session will be empty

 

add to the top of the page

<?php 
session_start();

Thanks! I added this at the top of the page and it worked :D Thanks alot  ;)

edit: Oh and also, another question, how do i put the values in diffirent places?

  Quote

if you use the

$user = mysql_fetch_assoc($doQuery);

approach, you would be able to access the data simply by calling the $user['id'] at the top (of course after the query) and $user['points'] at the bottom where you want to print it out

It worked. Thanks again  :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.