Jump to content

Get the results from database by ID


MuphN
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hello. I need some help making a script (udnerstanding it).

So I need to get for exemple "Thing" from detabase  by salecting id witch user is logged in now.

 

This is to check if user is logged in.

function login_check($mysqli) {
    // Check if all session variables are set 
    if (isset($_SESSION['user_id'], 
                        $_SESSION['username'], 
                        $_SESSION['login_string'])) {
 
        $user_id = $_SESSION['user_id'];
        $login_string = $_SESSION['login_string'];
        $username = $_SESSION['username'];
 
        // Get the user-agent string of the user.
        $user_browser = $_SERVER['HTTP_USER_AGENT'];
 
        if ($stmt = $mysqli->prepare("SELECT password 
                                      FROM members 
                                      WHERE id = ? LIMIT 1")) {
            // Bind "$user_id" to parameter. 
            $stmt->bind_param('i', $user_id);
            $stmt->execute();   // Execute the prepared query.
            $stmt->store_result();
 
            if ($stmt->num_rows == 1) {
                // If the user exists get variables from result.
                $stmt->bind_result($password);
                $stmt->fetch();
                $login_check = hash('sha512', $password . $user_browser);
 
                if ($login_check == $login_string) {
                    // Logged In!!!! 
                    return true;
                } else {
                    // Not logged in 
                    return false;
                }
            } else {
                // Not logged in 
                return false;
            }
        } else {
            // Not logged in 
            return false;
        }
    } else {
        // Not logged in 
        return false;
    }
}

And I need to get the the thing from detabase table named Members. and name it a $.

I know how to get it, but I dont know how to get from logged id now. (ID). For exemple user loggs in and it should show Hes satus from thing.

Link to comment
Share on other sites

There already is a query that is using the users_id to get the password. You can change this query so it gets both the password and Thing

 

To do this change   $mysqli->prepare("SELECT password   to   $mysqli->prepare("SELECT password, Thing

 

Not to assign Thing to a variable change  $stmt->bind_result($password);   to   $stmt->bind_result($password, $thing);

 

$thing  now holds the value in the Thing column for current logged in user.

Link to comment
Share on other sites

a function who's name is login_check() should only check if the current user is logged in. the purpose of that function is not to retrieve any arbitrary user information. the user information it happens to retrieve are for the purpose of authenticating the user.

 

after your code calls login_check() and it returns true, you know that the current user is logged in and you can use his $_SESSION['user_id'] to then query for any information you have stored for that user.

Link to comment
Share on other sites

so should I create new function?  and use it on other?

function getThing($mysqli) {

    if ($stmt = $mysqli->prepare("SELECT Thing FROM members WHERE id = ? LIMIT 1")) {

 
            $stmt->bind_param('i', $Thingy);
            $stmt->execute();   // Execute the prepared query.
            $stmt->store_result();
            if ($stmt->num_rows == 1) {
                // If the user exists get variables from result.
                $stmt->bind_result( $Thing);
                $stmt->fetch();
}
 
hmmm I still dont understand this quet well. I think I made up a nonsense for this function.
But if it would be ok then I would echo getThing; ??
Link to comment
Share on other sites

  • Solution
so should I create new function?

 

Yes. Do it in another function. 

 

Your code for the function is almost there, a few changes are required. First you need to get the user_id from $_SESSION['user_id'] not $Thingy

$stmt->bind_param('i', $_SESSION['user_id']); // pass users id to query

Now change $stmt->fetch(); to

                $stmt->fetch();
                return $Thing; # return users Thing value
          } # close if for $stmt->num_rows
    } # close if for $stmt = $mysqli->prepare

    return false; # return false

When you call getThing() you'd do

$usersThing = getThing($mysqli); // get the users thing
Edited by Ch0cu3r
Link to comment
Share on other sites

function getThing($mysqli) {

    if ($stmt = $mysqli->prepare("SELECT Thing FROM members WHERE id = ? LIMIT 1")) {

            $user_id = $_SESSION['user_id'];
            $stmt->bind_param('i', $user_id);
            $stmt->execute();
            $stmt->store_result();
            if ($stmt->num_rows == 1) {
                $stmt->bind_result( $Thing);
                $stmt->fetch();
                return $Thing;
        }
    }
  return false;
}
 

$usersThing = getThing($mysqli); // get the users thing

 

Then I should use 

echo $usersThing; as I understood? 

Edited by MuphN
Link to comment
Share on other sites

what is return false; for in that function? i mean, If the function called it should show, there is no else.

OR Should I make
$usersThing = getThing($mysqli);

if ($usersThing === True) {

   return true;

}else{

  return false;

 

If that works. hmmm

Link to comment
Share on other sites

The function will return false when the prepared statement fails or the query did not return a result. It will only return the value of $thing when a result is returned from the query.

 

You can add an else if you want, but it is optional in this case. Another way to code the function

     if ($stmt = $mysqli->prepare("SELECT Thing FROM members WHERE id = ? LIMIT 1")) {
            $user_id = $_SESSION['user_id'];
            $stmt->bind_param('i', $user_id);
            $stmt->execute();
            $stmt->store_result();
            if ($stmt->num_rows == 1) {
                $stmt->bind_result( $Thing);
                $stmt->fetch();
                return $Thing;
        } else {
            return false; // query did not return 1 row
        } 
    } else {
        return false; // prepared statement failed
    }

Outcome will still be the same

Edited by Ch0cu3r
Link to comment
Share on other sites

your current function is tied to one specific id, the currently logged in user's id. to make a general purpose function that could retrieve data for any user's id, for example in a game, a profile page, a pm system, the id should be a call time parameter into the function.

 

then to use the general purpose function specifically for the currently logged in user you would call it -

$thing = getThing($mysqli,$_SESSION['user_id']);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.