DarthJawns Posted April 4, 2020 Share Posted April 4, 2020 I'm working on the user area of my site and I'm trying to get it to display how much currency and the rank the user currently has. I know sql statement would be something like "select * from users where username = ?" but I'm a little lost on how I bind the session username so it knows what username to pull from the users table. Quote Link to comment Share on other sites More sharing options...
MJ12 Posted April 5, 2020 Share Posted April 5, 2020 What language / framework are you using? Quote Link to comment Share on other sites More sharing options...
DarthJawns Posted April 5, 2020 Author Share Posted April 5, 2020 2 minutes ago, MJ12 said: What language / framework are you using? Just procedural php Quote Link to comment Share on other sites More sharing options...
MJ12 Posted April 5, 2020 Share Posted April 5, 2020 You'd want to use a prepared statement SELECT currency, rank FROM users WHERE name = ? LIMIT 1; where ? is replaced by the username as a bound variable. An example of this is shown here: https://www.w3schools.com/php/php_mysql_prepared_statements.asp. Quote Link to comment Share on other sites More sharing options...
DarthJawns Posted April 5, 2020 Author Share Posted April 5, 2020 11 minutes ago, MJ12 said: You'd want to use a prepared statement SELECT currency, rank FROM users WHERE name = ? LIMIT 1; where ? is replaced by the username as a bound variable. An example of this is shown here: https://www.w3schools.com/php/php_mysql_prepared_statements.asp. How would I echo the results then? Quote Link to comment Share on other sites More sharing options...
MJ12 Posted April 5, 2020 Share Posted April 5, 2020 You'd bind the output of the SQL statement to a variable and then echo it normally, I'd imagine. I haven't written PHP in many years, so you'd probably echo each column out as echo $row['column_name']; or something like that. Quote Link to comment Share on other sites More sharing options...
Hare Posted April 5, 2020 Share Posted April 5, 2020 (edited) $query = mysqli_query($connect, "SELECT * FROM users WHERE username = '$username'"); $row = mysqli_fetch_array($query); $usermoney = $row["money"]; This will get the user based on their username, and allow you to display their money. echo $usermoney; If it worked, you'll be able to display the user's money like this. This is all assuming that you have the user's name contained in the $username variable. If you don't have $username set, it won't work. You can set $username with a session or cookie. This also assumed you have your PHP/MYSQL database connection set up and contained in the $connect variable (or whatever you named yours). Edited April 5, 2020 by Hare Quote Link to comment Share on other sites More sharing options...
DarthJawns Posted April 5, 2020 Author Share Posted April 5, 2020 (edited) @Hare , this is what I currently have <? ini_set('display_errors', 1); require_once "header.php"; $sql = "SELECT * FROM users WHERE username = ?"; if($stmt = mysqli_prepare($link, $sql)){ mysqli_stmt_bind_param($stmt, 's', $_SESSION['username']); if(mysqli_stmt_execute($stmt)){ $info = mysqli_fetch_array($stmt); echo "Current Points:" . $info['points']; } else { echo "Can't find user"; } } mysqli_stmt_close($stmt); ?> but get an error message of mysqli_fetch_array() expects parameter 1 to be mysqli_result Edited April 5, 2020 by DarthJawns Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.