So in my users table there are 5 columns. Id, username, password, email and money.
The money column is an int which is supposed to show how much money that user has. (I'm making a game).
I'm trying to make a script which displays how much money the user in the current session has, but my script displays the money for every user.
My code:
<?php session_start(); ?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database4";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['money'];
}
} else {
echo "You have no items yet!";
}
$conn->close();
?>
How would I do this?
You've missing where clause in your select query.
it would be something like:
$sql = "SELECT * FROM users WHERE id='{$_SESSION['user_id']}'";
$result = $conn->query($sql);
You just need to replace $_SESSION['user_id']
with the user id you are storing while user gets logged in.
EDIT:
Make sure you have session_start();
where page starts, additionally you may create unique index on email column in users table.
$sql = "SELECT * FROM users WHERE email='{$_SESSION['email']}'";
$result = $conn->query($sql);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments