Jump to content

Retrieving and displaying data from database


Recommended Posts

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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 by Hare
Link to comment
Share on other sites

@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 by DarthJawns
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...