Hazukiy Posted April 2, 2013 Share Posted April 2, 2013 Hi, how would I make it so that my page returns and echos database information? I've used the mysqli method in most of my coding, examples below and basically I want it so that it querys the firstname and lastname to the correct session id. I managed to do something like this before by doing a $row search but the problem with that was that it was returning all of the first and lastnames not one that matches the id to the database. I know you can do $row searches but I'm not too sure on those and the ones that were shown are not safe as it displays things like the database name, database password ect in that document where I've done an include like > include 'dbConfig.php'; (Also please note even though it hasn't got any includes, that's because the pages themselves are includes and all the includes that are needed such as the database one, I've declared on previous pages.) How I would like it to work: <?php Do some kind of safe row search up here. if(<USERS ID IS VALID THEN DO THIS>) { Echo the firstname and lastname here. } ?> How I've tried to do it: <?php if ($stmt = $mysqli->prepare("SELECT id, firstname, lastname FROM users WHERE firstname = ? AND lastname = ? LIMIT 1")) { $stmt->execute(); $stmt->store_result(); $stmt->bind_result($user_id, $firstname, $lastname); $stmt->fetch(); if($stmt->num_rows == 1) { $_SESSION['user_id'] = $user_id; $_SESSION['firstname'] = $firstname; $_SESSION['lastname'] = $lastname; echo $firstname; echo $lastname; return true; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/ Share on other sites More sharing options...
requinix Posted April 2, 2013 Share Posted April 2, 2013 You have placeholders in the query but you never bind anything to them. Gotta put values in there. Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/#findComment-1422493 Share on other sites More sharing options...
PaulRyan Posted April 2, 2013 Share Posted April 2, 2013 You are trying to return the users "firstname" and "lastname", by querying their "firstname" and "lastname". Should you be using the "id" to return the data you want. Use the $user_id variable to search the database and return the data. Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/#findComment-1422529 Share on other sites More sharing options...
Hazukiy Posted April 3, 2013 Author Share Posted April 3, 2013 You are trying to return the users "firstname" and "lastname", by querying their "firstname" and "lastname". Should you be using the "id" to return the data you want. Use the $user_id variable to search the database and return the data. Yeah, I wanted it so that when they login it returns their First and Lastname depending on the ID. Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/#findComment-1422634 Share on other sites More sharing options...
PaulRyan Posted April 3, 2013 Share Posted April 3, 2013 The WHERE in your query should be searching against id and not firstname and lastname, you need to change it. Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/#findComment-1422665 Share on other sites More sharing options...
Hazukiy Posted April 3, 2013 Author Share Posted April 3, 2013 The WHERE in your query should be searching against id and not firstname and lastname, you need to change it. Ok so instead of this: "SELECT id, firstname, lastname FROM users WHERE firstname = ? AND lastname = ? LIMIT 1" I should change it to this?: "SELECT id, firstname, lastname FROM users WHERE id = ? LIMIT 1" Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/#findComment-1422740 Share on other sites More sharing options...
PaulRyan Posted April 3, 2013 Share Posted April 3, 2013 Give it a try? Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/#findComment-1422742 Share on other sites More sharing options...
Hazukiy Posted April 3, 2013 Author Share Posted April 3, 2013 (edited) Give it a try? Aha done it. I removed the = ?, my fault, was being stupid xD Thanks for your help ^.^ EDIT: Ok I've noticed a problem, it's returning only 1 record, so i'm only getting one name, its not getting the name depending on the ID :/ Edited April 3, 2013 by Hazukiy Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/#findComment-1422744 Share on other sites More sharing options...
PaulRyan Posted April 3, 2013 Share Posted April 3, 2013 When using "WHERE id = ?" you need to bind the id to ?. Where is the user's id coming from? A session variable? Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/#findComment-1422747 Share on other sites More sharing options...
Hazukiy Posted April 3, 2013 Author Share Posted April 3, 2013 When using "WHERE id = ?" you need to bind the id to ?. Where is the user's id coming from? A session variable? Well I have this as my session but I would like it to get the name through the database ID. So when they login, it's getting their database ID. function sec_session_start() { $session_name = 'sec_session_id'; $secure = false; $httponly = true; ini_set('session.use_only_cookies', 1); $cookieParams = session_get_cookie_params(); session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly); session_name($session_name); session_start(); session_regenerate_id(true); } Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/#findComment-1422750 Share on other sites More sharing options...
PaulRyan Posted April 4, 2013 Share Posted April 4, 2013 You should have explained your question a bit better, but we got their in the end. You need to create a log in form, then check their log in details against the database. I assumed you had them logged in, but couldn't get the data you wanted back. Quote Link to comment https://forums.phpfreaks.com/topic/276420-get-firstname-lastname-through-session-id/#findComment-1422794 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.