PHP echo user data

bnmz4

New Member
Jul 18, 2012
10
0
hey guys,



So in my db I've got 5 tables "I'd, username, password, email, credits" and what I want to do is echo the users `credits` count, but could you guys possibly tell me how to do this?



I'f you don't understand what I mean:

1)user logs in

2)on their page they can see how many points they have



Thankyou
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
PHP:
<?php
/* Database Connection (MySQLi) */
$conn =new mysqli('mysqlhost','mysqlusername','mysqlpassword','databasename');
$query = $conn->query("SELECT * FROM users");
$result = $query->fetch_assoc();

/* Make Database Into Variables */
$ID = $result['id'];
$Username = $result['username'];
$Password = $result['password'];
$Email = $result['email'];
$NoCredits = $result['credits'];

/* Display */
// echo($ID); // ID
// echo($Username); // Username
// echo($Password); // Password
// echo($Email); // Email
echo($NoCredits); // Number of credits
?>

Change
Code:
users
to whatever the table name is.
 

bnmz4

New Member
Jul 18, 2012
10
0
it does get the credits amount but then its the same for all users, could you possibly fix that?

so it only shows how many the user who is logged in ahs?
 
Last edited by a moderator:

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
PHP:
<?php
/* Database Connection (MySQLi) */
$conn =new mysqli('mysqlhost','mysqlusername','mysqlpassword','databasename');
$query = $conn->query("SELECT * FROM users WHERE id = '" . $userid . "'");
$result = $query->fetch_assoc();

/* Make Database Into Variables */
$ID = $result['id'];
$Username = $result['username'];
$Password = $result['password'];
$Email = $result['email'];
$NoCredits = $result['credits'];

/* Display */
// echo($ID); // ID
// echo($Username); // Username
// echo($Password); // Password
// echo($Email); // Email
echo($NoCredits); // Number of credits
?>

Change $userid to whatever you have called the variable that holds the users ID.
 

bnmz4

New Member
Jul 18, 2012
10
0
<?php
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ">
<html xmlns=" ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="<?php echo $design; ?>/style.css" rel="stylesheet" title="Style" />

</head>
<body>
<div class="header">
<a href="<?php echo $url_home; ?>"><img src="<?php echo $design; ?>/images/logo.png" alt="Members Area" /></a>
</div>
<?php
//We check if the user is logged
if(isset($_SESSION['username']))
{
//We check if the form has been sent
if(isset($_POST['username'], $_POST['password'], $_POST['passverif'], $_POST['email'], $_POST['avatar']))
{


if (!$user_row)
{
// User does not exist
}?>
</div>
<?php
}
}
else
{
?>
<div class="message">To access this page, you must be logged.<br />
<a href="connexion.php">Log in</a></div>
<?php
}
?>


<?php
/* Database Connection (MySQLi) */
$conn =new mysqli('localhost','root','fartface1','habbocreds');
$query = $conn->query("SELECT * FROM users WHERE id = '" . $userid . "'");
$result = $query->fetch_assoc();

/* Make Database Into Variables */
$ID = $result['id'];
$Username = $result['username'];
$Password = $result['password'];
$Email = $result['email'];
$NoCredits = $result['credits'];


/* Display */
// echo($ID); // ID
// echo($Username); // Username
// echo($Password); // Password
// echo($Email); // Email

?>


<div class="foot"><a href="<?php echo $url_home; ?>">You Have <?php echo($NoCredits);?> credits available to spend!




</html>



this is my code so what shall i change $userid to?
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
Sorry, but the above code does not make sense... How can the user not exist if he's logged in (as you check if the user exists AFTER you check if the username session exists, which says you check if the user is logged in. But if he's logged in, he does exists for sure)? Are you sure this is the correct code?
 

bnmz4

New Member
Jul 18, 2012
10
0
its because if the user tries to go directly to that page it gives them an error beleive me it works

anyone tell me how to put $userid into a variable?to use in the credits code?
 
Last edited by a moderator:

bnmz4

New Member
Jul 18, 2012
10
0
PHP:
<?php
/* Database Connection (MySQLi) */
$conn =new mysqli('localhost','root','fartface1','habbocreds');
$query = $conn->query("SELECT * FROM users WHERE id = '" . $userid . "'");
$result = $query->fetch_assoc();

$query = $conn->query("SELECT * FROM users WHERE id = '" . $userid . "'");

that $userid i need a variable for it to show the user point count for a specific user from the db, it needs to grab the users id so it shows their points
 

GarettM

Posting Freak
Aug 5, 2010
833
136
PHP:
<?php
/* Database Connection (MySQLi) */
$conn =new mysqli('localhost','root','fartface1','habbocreds');
$query = $conn->query("SELECT * FROM users WHERE id = '" . $userid . "'");
$result = $query->fetch_assoc();

$query = $conn->query("SELECT * FROM users WHERE id = '" . $userid . "'");

that $userid i need a variable for it to show the user count for a specific user from the db
PHP:
$account = function($user_id = 0) {
   if($user_id > 0 && is_int($user_id))
   {
     $connection = new mysqli('localhost','root','fartface1','habbocreds');
    
     $result   = @$connection->query('SELECT * FROM users WHERE id = "'. $user_id .'";')->fetch_assoc(MYSQLI_BOTH);
     if(!isset($result) && !is_array($result))
     {
       // account was not found.
       return false;
     }
     return $result;
   }
};
$user_info = function($id) {
   // $userid = array_key_exists('user_id', $_SESSION) ? $_SESSION['user_id'] : 0;
   $userid   = is_int($_POST['id']) ? $_POST['id'] : 0;
  
   global $account;
   if( ($data = $account($user_id)) != false )
   {
     //return user data.
     return $data[$id];
   } else {
    return 0;
   }
}
if($_POST['submit'])
   echo 'User has ' . $user_info['coins'] . 'coins';
Something like that?
or
PHP:
$userid = is_int($_GET['id']) ? $_GET['id'] : 0;
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
PHP:
<?php
$conn = new mysqli('mysqlhost', 'mysqlusername', 'mysqlpassword', 'databasename');
$query = $conn->query("SELECT SUM(credits) AS total_credits FROM users");
$result = $query->fetch_assoc();

echo $result['total_credits'];
?>
 

GarettM

Posting Freak
Aug 5, 2010
833
136
PHP:
<?php
$conn = new mysqli('mysqlhost', 'mysqlusername', 'mysqlpassword', 'databasename');
$query = $conn->query("SELECT SUM(credits) AS total_credits FROM users");
$result = $query->fetch_assoc();

echo $result['total_credits'];
?>
That's everyone's credits combined I believe he wants per user. Not sure though
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
Hmmm, misread his comment.

Could you please state which CMS you are using? Maybe a .zip file for it? Currently we are quite guessing in the dark, and we also make a new SQL session whilest I'm sure it's already there.
 

Users who are viewing this thread

Top