Hazukiy Posted April 4, 2013 Share Posted April 4, 2013 Hi, I'm just wondering but how would I go about making a query so that I can fetch two valid pieces of information and display them? So In this case I would like to query my database to find the correct Firstname and Lastname of that use through their session? Here's something I made, it works! But it only fetches the first result and doesn't return the correct name for the correct user. <?php if ($stmt = $mysqli->prepare("SELECT id, firstname, lastname FROM users WHERE id 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 ' '; echo $lastname; } } ?> So at this point it's displaying two names but they are the first record in my database and it's not displaying the correct name for the correct user so for example.: I'm user 6 in the database with the name John Doe but it's displaying record 1 in the database which is Luke Void, basically I want it so when John Doe logs into his account, it says on the screen 'John Doe', so it matches the correct client. Help on this one please, I'm kind of newish to databases... thanks Quote Link to comment https://forums.phpfreaks.com/topic/276512-get-firstname-lastname-from-database/ Share on other sites More sharing options...
mac_gyver Posted April 4, 2013 Share Posted April 4, 2013 in your existing thread for this, PaulRyan pointed out what you need to do to make it work - When using "WHERE id = ?" you need to bind the id to ?. Where is the user's id coming from? A session variable? the query you posted in this thread is just testing where the id is a true value and returning the first row. Quote Link to comment https://forums.phpfreaks.com/topic/276512-get-firstname-lastname-from-database/#findComment-1422844 Share on other sites More sharing options...
Hazukiy Posted April 4, 2013 Author Share Posted April 4, 2013 in your existing thread for this, PaulRyan pointed out what you need to do to make it work - the query you posted in this thread is just testing where the id is a true value and returning the first row. Ah yeah sorry, still kind of getting my head around mySQL databases ect but how would I 'Bind' the 'id' to '?' Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/276512-get-firstname-lastname-from-database/#findComment-1422860 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.