MuphN Posted January 25, 2014 Share Posted January 25, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/ Share on other sites More sharing options...
Ch0cu3r Posted January 25, 2014 Share Posted January 25, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466525 Share on other sites More sharing options...
MuphN Posted January 25, 2014 Author Share Posted January 25, 2014 So after this I could just do echo $Thing; right and it will print the Thing from the detabase witch is from the right User ID - Logged in now? Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466526 Share on other sites More sharing options...
mac_gyver Posted January 25, 2014 Share Posted January 25, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466528 Share on other sites More sharing options...
Ch0cu3r Posted January 25, 2014 Share Posted January 25, 2014 (edited) MuphN Yes, however with your code as-is (with the changes I suggested) $thing will only be accessible from within the login_check() function. You wont be able to use this variable outside of this function. This is due to variable scope. Edited January 25, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466529 Share on other sites More sharing options...
MuphN Posted January 25, 2014 Author Share Posted January 25, 2014 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; ?? Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466532 Share on other sites More sharing options...
Solution Ch0cu3r Posted January 25, 2014 Solution Share Posted January 25, 2014 (edited) 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 January 25, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466533 Share on other sites More sharing options...
MuphN Posted January 25, 2014 Author Share Posted January 25, 2014 (edited) 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 January 25, 2014 by MuphN Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466534 Share on other sites More sharing options...
Ch0cu3r Posted January 25, 2014 Share Posted January 25, 2014 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466537 Share on other sites More sharing options...
MuphN Posted January 25, 2014 Author Share Posted January 25, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466538 Share on other sites More sharing options...
Ch0cu3r Posted January 25, 2014 Share Posted January 25, 2014 (edited) 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 January 25, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466541 Share on other sites More sharing options...
MuphN Posted January 25, 2014 Author Share Posted January 25, 2014 Okey I understand it now. Thanks again for saving my script. Gratefull!! )) and thx mac_graver for taking your time to help me aswell! Awesome. Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466542 Share on other sites More sharing options...
mac_gyver Posted January 25, 2014 Share Posted January 25, 2014 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']); Quote Link to comment https://forums.phpfreaks.com/topic/285669-get-the-results-from-database-by-id/#findComment-1466548 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.