Search the Community
Showing results for tags 'firstname'.
-
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
-
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; } } ?>