Jump to content

Displaying MySQL table information for logged in user


Sprout

Recommended Posts

I want my users to be able to see how many points they have when they go to their user info page.

 

My MySQL database is named "webvilla_users" and the table below is called "users"

 

1277976042.jpg

 

I want my user to be able to view their number of points when they view their account info.

 

So if anybody can explain to me how to retrieve the logged in user's point number from the database and display it for them to see I'd appreciate it. This is what my user profile script looks like if it gives you a better idea of how users view their information I modified it from a tutorial.

 

<?
include("include/session.php");
?>

<html> 

<head> 

<script type="text/javascript">
function roll_over(img_name, img_src)
   {
   document[img_name].src = img_src;
   }
</script>

<STYLE TYPE="text/css">
table {border-spacing:0;}
table {border-collapse:collapse;}
img {vertical-align:top}
img {display:block;}
</style>

<title>Docoden</title> 


<STYLE TYPE="text/css"> 

a:link {text-decoration: none; color: #0099FF;}
a:visited {text-decoration: none; color: #0099FF;}
a:active {text-decoration: none; color: #2FB0F4;}
a:hover {text-decoration: none; color: #2FB0F4;}

p { font-family: arial; } 
body {
font-family: arial; 
font-size: 14px;
}

div.wrapper {
position: relative;
width: 900px;
margin-left: auto;
margin-right: auto;
}
div.top {
position: relative;
width: 900px;
height: 20px;
     background-image:  url(top.png);
     layer-background-image: url(top.png);
}
div.bottom {
position: relative;
width: 900px;
height: 20px;
     background-image:  url(bottom.png);
     layer-background-image: url(bottom.png);
}

div.left {
float: left;
position: relative;
width: 20px;
     background-image:  url(left.png);
     layer-background-image: url(left.png);
}
div.center {
float: left;
position: relative;
background-color: #FFCC66;
}
div.right {
float: right;
position: relative;
width: 20px;
     background-image:  url(right.png);
     layer-background-image: url(right.png);
}
div.thin {
position: relative;
width: 900px;
height: 45px;
     background-image:  url(footer.png);
     layer-background-image: url(footer.png);
</style> 

</head> 
<body bgcolor="CFECEC"> 
<div class="wrapper"> 
<table width="100%" height="100%"> 
<tr> 
<td height="10%"> 

<div style="width:100%" align="center">
<img src="docodenlogo.png">
</div> 
</td> 
</tr> 
<tr> 
<td> 
<div style="height:5;"></div>
<div class="thin"><table width="100%" height="100%" border="0"> 
<tr> 
<td>

<?
/**
  * User has already logged in, so display relavent links, including
* a link to the admin center if the user is an administrator.
*/
if($session->logged_in){
   echo "<table><tr><td>  <font size=\"4\"><b>$session->username</b></font></td>";
if($session->isAdmin()){
      echo "<td>|</td><td><a href=\"admin/admin.php\"><img src=\"admin-center1.png\"
    onmouseover=\"this.src = 'admin-center2.png';\"
    onmouseout=\"this.src = 'admin-center1.png';\" border=\"0\"
/>
</A></td>";
   }

   echo "<td width=\"5\">|</td><td> <a href=\"userinfo.php?user=$session->username\"my-account1.png\"><img src=\"my-account1.png\"
    onmouseover=\"this.src = 'my-account2.png';\"
    onmouseout=\"this.src = 'my-account1.png';\" border=\"0\"
/>
</A></td>"
       ."<td width=\"5\">|</td><td> <a href=\"useredit.php\"><img src=\"edit-account1.png\"
    onmouseover=\"this.src = 'edit-account2.png';\"
    onmouseout=\"this.src = 'edit-account1.png';\" border=\"0\"
/></A></td>"
."<td width=\"5\">|</td><td><a href=\"process.php\">
<img src=\"logout1.png\"
    onmouseover=\"this.src = 'logout2.png';\"
    onmouseout=\"this.src = 'logout1.png';\" border=\"0\"
/>
</A></td><td width=\"5\">|</td></tr></table>";
}
else{
?>

<?
/**
* User not logged in, display the login form.

* If user has already tried to login, but errors were
* found, display the total number of errors.
* If errors occurred, they will be displayed.
*/
if($form->num_errors > 0){
   echo "";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3" width="100%">
<tr><td width="65">   <font size="4"><b>Sign in:</b></font></td><td width="10"><input type="checkbox" title="Remember Login" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>></td><td width="100"><input title="Username" style="width:100px;" type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td><td width="100"><input title="Password" style="width:100px;" type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td width="0"><td><? echo $form->error("pass"); ?></td><td><a href="forgotpass.php" title="Forgot your password?"><img src="qm1.png"
    onmouseover="this.src = 'qm2.png';"
    onmouseout="this.src = 'qm1.png';" border="0"
/></font></td><td  align="left">
<input type="hidden" name="sublogin" value="1">
<input
    type="image"
    src="login1.png"
    onmouseover="this.src = 'login2.png';"
    onmouseout="this.src = 'login1.png';"
/> </td><td width="80%">
</td>
<td align="right">

<a href="register.php">
<img src="join-us1.png"
    onmouseover="this.src = 'join-us2.png';"
    onmouseout="this.src = 'join-us1.png';" border="0"
/>
</A></td><td></td></tr>
</table>
</form>

<?
}

echo "";

?>




</td> 
</tr> 
</table> 
</div> 



<div style="height:5;"></div> 
<div class="top"></div><div class="middle" style="height:400;"> 
<div class="left" style="height:400;"></div> 
<div class="center" style="height:400; width:860;"> 
<table width="100%" height="100%" border="0">  
<tr> 
<td valign="top">




<?
/* Requested Username error checking */
$req_user = trim($_GET['user']);
if(!$req_user || strlen($req_user) == 0 ||
   !eregi("^([0-9a-z])+$", $req_user) ||
   !$database->usernameTaken($req_user)){
   die("Username not registered");
}

/* Logged in user viewing own account */
if(strcmp($session->username,$req_user) == 0){
   echo "<table width=\"100%\" height=\"100%\" valign=\"top\" border=\"0\"><tr><td align=\"center\" valign=\"top\"><b><font size=\"6\">$session->username's Account</font></b></td></tr></table>";
echo "<b>Email:</b> ".$req_user_info['email']."<br>";
}
/* Visitor not viewing own account */
else{
   echo "<table width=\"100%\" height=\"100%\" border=\"0\"><tr><td align=\"center\" valign=\"top\"><b><font size=\"6\">$session->username's Account</font></b></td></tr></table>";

echo "<b>Email:</b> ".$req_user_info['email']."<br>";
}

?>

</td>
</tr>
</table> 

</div> 
<div class="right" style="height:400;"></div> 
</div> 
<div class="bottom"></div> 

<div style="height:5;"></div> 

<div class="thin"><table width="100%" height="100%"> 
<tr> 
<td align="center"> 
<b>© 2010 Docoden</b> 
</td> 
</tr> 
</table> 
</div> 
</td> 
</tr> 
<tr> 
<td height="10%"> 
</td> 
</tr> 
</table> 
</div> 



</body> 
</html>

 

Providing you have userid stored within the $_SESSION array something like this will do the trick.

 

<?php

session_start();

$sql = "SELECT points FROM users WHERE userid = {$_SESSION['userid']} LIMIT 1";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    $row = mysql_fetch_assoc($result);
    echo "You have {$row['points']} points";
  }
}

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.