I have a website, and the user has just logged in and it redirects them to the User Dashboard. I have the user_id in a SESSION so I can use that for my SELECT queries to grab other information about the user.
My question is, how can I use Prepared Statments to just grab the variables that I need for that page, without using a foreach statement or a while statement. It will just be for that specific user logged in, not displaying information for multiple users.
Every example I look up is like this:
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";
if ($stmt = $mysqli->prepare($query)) {
/* execute statement */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($name, $code);
/* fetch values */
while ($stmt->fetch()) {
printf ("%s (%s)\n", $name, $code);
}
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
Does it have to be in a while statement? I just need the variables at the top, so I can display my regular page and then use the variables when I need to. Does that make sense?
Just looking to learn more and get a better understanding of how this works.
If you just need the top record:
$stmt->fetchwill fetch the first row, you don’t need thewhileunless you want to continue through all the rows.