Retrieving and displaying data from database

DarthJawns

New member
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. 

 
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.

 
$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).

 
Last edited by a moderator:
@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

 
Last edited by a moderator:
Back
Top