Jump to content

[SOLVED] how to show data from tables for certain users


Recommended Posts

i have the code to show data in the tables...but if there's 3 users signed up in the table three fields will show up..is there a way i  can make it so the table only shows the fields for the user that's logged in only? like so the user only see's his own field?

no problem....

heres the stats code that shows the users stats..

<!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=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {
font-size: 18px;
font-weight: bold;
color: #FF0000;
}
.style2 {
font-size: 16px;
font-weight: bold;
color: #000000;
}
-->
</style>
</head>

<body>
<p class="style1">STATS:</p>
<p class="style2">Gold: 
<?
mysql_connect ("localhost","root","");
mysql_select_db ("game");

$sql = "select * from users";
       $result = mysql_query ($sql);

       while ($row = mysql_fetch_array($result))
              {
              $gold= $row["gold"];

echo "$gold<br>";
              }
?>  
</p>
<p class="style2">Water: <?php 
mysql_connect ("localhost","root","");
mysql_select_db ("game");

$sql = "select * from users";
       $result = mysql_query ($sql);

       while ($row = mysql_fetch_array($result))
              {
              $water= $row["water"];

echo "$water<br>";
              }
?></p>
<p class="style2">Food: <?php
mysql_connect ("localhost","root","");
mysql_select_db ("game");

$sql = "select * from users";
       $result = mysql_query ($sql);

       while ($row = mysql_fetch_array($result))
              {
              $food= $row["food"];

echo "$food<br>";
              }
?></p>
<p class="style2">Health: <?php 
mysql_connect ("localhost","root","");
mysql_select_db ("game");

$sql = "select * from users";
       $result = mysql_query ($sql);

       while ($row = mysql_fetch_array($result))
              {
              $health= $row["health"];

echo "$health<br>";
              }
?></p>
<p class="style2">Points: <?php 
mysql_connect ("localhost","root","");
mysql_select_db ("game");

$sql = "select * from users";
       $result = mysql_query ($sql);

       while ($row = mysql_fetch_array($result))
              {
              $points= $row["points"];

echo "$points<br>";
              }
?></p>
<p class="style2"> </p>
</body>
</html>

Well one big problem you have right now is you are doing the exact same query multiple times, pulling all the columns each time, but only displaying 1 of them.  Why not display all your items from the same query?

 

Instead of just doing SELECT * FROM users, why not change it to SELECT * FROM users WHERE userid = currentuserid

 

That way you only get the current user's information. The kicker is you need to know the userid, so like rev said, use a session when the user logs in to store his id and pull it out.

 

session_start should provide examples.

so i should add WHERE userid = currentuserid to this part...select * from users and then i should create a session and thats it?

 

Your missing the point, you need to define the userid on login in session. Then when you call that query use the session variable of userid in place of currentuserid.

 

That will allow you to pull up the records only for that user id. Given that I am not sure how your script is suppose to work or does work, it may already be done. But also as rev pointed out I would use just one query as the first query will give you the items you need, no need for multiples.

Only because I am bored, I decided to help a little. This is assuming that you do have a column for userid in table users:

 

<!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=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {
   font-size: 18px;
   font-weight: bold;
   color: #FF0000;
}
.style2 {
   font-size: 16px;
   font-weight: bold;
   color: #000000;
}
-->
</style>
</head>

<body>
<p class="style1">STATS:</p>
<p class="style2">Gold:
<?php
mysql_connect ("localhost","root","");
mysql_select_db ("game");

$sql = "select * from users WHERE userid = " . intval($_GET['userid']);
       $result = mysql_query ($sql);

       while ($row = mysql_fetch_array($result))
              {
              $gold= $row["gold"] . "<br />";
		 $water= $row["water"] . "<br />";
		 $food= $row["food"] . "<br />";
		 $health= $row["health"] . "<br />"
		 $point= $row["points"] . "<br />"

              }
		  echo "$gold<br>";
?> 
</p>
<p class="style2">Water: <?php 
echo "$water<br>";
?></p>
<p class="style2">Food: <?php
echo "$food<br>";
?></p>
<p class="style2">Health: <?php 
echo "$health<br>";
?></p>
<p class="style2">Points: <?php 
echo "$points<br>";
?></p>
<p class="style2"> </p>
</body>
</html>

 

That should help you out, I did $_GET for demonstration purposes, but that can be changed later on to SESSION when you get it setup.

<!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=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {
   font-size: 18px;
   font-weight: bold;
   color: #FF0000;
}
.style2 {
   font-size: 16px;
   font-weight: bold;
   color: #000000;
}
-->
</style>
</head>

<body>
<p class="style1">STATS:</p>
<p class="style2">Gold:
<?php
mysql_connect ("localhost","root","");
mysql_select_db ("game");

$sql = "select * from users WHERE userid = " . intval($_GET['userid']);
       $result = mysql_query ($sql);

       while ($row = mysql_fetch_array($result))
              {
              $gold= $row["gold"];
		 $water= $row["water"];
		 $food= $row["food"];
		 $health= $row["health"];
		 $point= $row["points"];

              }
		  echo "$gold<br>";
?> 
</p>
<p class="style2">Water: <?php 
echo "$water<br>";
?></p>
<p class="style2">Food: <?php
echo "$food<br>";
?></p>
<p class="style2">Health: <?php 
echo "$health<br>";
?></p>
<p class="style2">Points: <?php 
echo "$points<br>";
?></p>
<p class="style2"> </p>
</body>
</html>

 

Missed the ; for some values, should work.

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.