I think a basic little tutorial might be in order here.
First off: SELECT * FROM users
means: "give me everything in the users
table". You will get the full table, every row and every column.
while($row = mysqli_fetch_array($connection))
will loop through every row your query returns. It will call mysqli_fetch_array()
and put the result in $row
until there are no more rows in your query's result.
If you only want to output a single row of data, you have three options:
- Add a
WHERE
condition so that your query will only fetch a specific row
- Add a
LIMIT
clause so that your query will only fetch a single row
- Call
mysqli_fetch_array()
only once instead of in a while
loop
From the comments in the discussion thread, it looks like you want to retrieve only the balance
for the currently logged in user, and you have a session variable somewhere that tells you who that user is. That means you'll want to use a WHERE
condition so that your query will only fetch the row for that specific user.
You haven't told us what that session variables is called or what the name is of the column in the users
table that you can compare that session variable with, so I'll assume that your users
table has an id
column and your session variable is called user_id
and should match the id
value from your users
table.
So let's say the user with id
123 is currently logged in. You'll want to end up with the query SELECT balance FROM users WHERE id = 123
.
The quick solution is to change your code to:
$connection = mysqli_query($db, "SELECT balance FROM users WHERE id = " . $_SESSION['user_id']);
.
This is bad code. We'll make it better, but try this first and see if it gets you the result you actually want. If it doesn't, let me know.
The reason this is bad code is because adding variables to a query string like this dramatically increases the risk of SQL injections. If there's any possibility at all that the value of the variable comes from user input, then at some point a user will figure that out and make sure it contains something that will break your application.
- Best case scenario: the page simply won't render for that one user.
- Bad case scenario: the user will be able to read out your entire database and will sell sensitive user data to the highest bidder.
- Worst case scenario: the user will be able to inject some of their own Javascript code into your database in a column you're not sanitizing before rendering, letting them capture and intercept passwords and/or financial information your users are entering on your site and they will then use that information to make life miserable for all of your users.
So you don't want to just drop $_SESSION['user_id']
into your query like that. Instead, you'll want to use a prepared statement and let the database handle the problem of dropping the variable into the query.
First, you'll need to prepare
your query:
$statement = mysqli_prepare($db, "SELECT balance FROM users WHERE id = ?");
The ?
symbol is a placeholder where you can bind
a parameter. Let's do that:
$statement->bind_param("i", $_SESSION['user_id']);
The "i"
tells MySQL that you're binding an integer value. If you're not matching against a user id
but a username, for example, you'll want to instead use "s"
to tell MySQL you're binding a string value.
Now you can execute
the query and get the result. Putting it all together:
$statement = mysqli_prepare($db, "SELECT balance FROM users WHERE id = ?");
$statement->bind_param("i", $_SESSION['user_id']);
$statement->execute();
$connection = $statement->get_result();
Let us know if that works. Some tweaking might be required.