/    Sign up×
Community /Pin to ProfileBookmark

Which Suits My Purpose get_result Or bind_result ?

Php Folks,

You will notice that, I have a question on my comments where I show confusion on how to proceed forward. I ask which 1 of the following 2 I should use which will suit the context of my code well.
I commented-out the one that I personally thought did not fit into my codes’ context. But, I need your professional opinion.

//$result = mysqli_stmt_get_result($stmt); //Which line to use ? This line or the next ?

$result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // Which line to use ? This line or the one above ?

It is a Log In Page script where the users are given a choice to either login to their accounts by typing their “username” or “email”.

[code]
<?php

/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set(‘display_errors’, ‘1’);
ini_set(‘display_startup_errors’, ‘1’);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

include ‘config.php’;

// check if user is already logged in
if (is_logged() === true)
{
//Redirect user to homepage page after 5 seconds.
header(“refresh:2;url=home.php”);
exit; //
}

if ($_SERVER[‘REQUEST_METHOD’] == “POST”)
{
if (isset($_POST[“login_username_or_email”]) && isset($_POST[“login_password”]))
{
$username_or_email = trim($_POST[“login_username_or_email”]); //
$password = $_POST[“login_password”];

//Select Username or Email to check against Mysql DB if they are already registered or not.
$stmt = mysqli_stmt_init($conn);

if(strpos(“$username_or_email”, “@”))
{
$email = $username_or_email;
$username = “”;

$query = “SELECT ids, usernames, passwords, emails, accounts_activations_statuses FROM users WHERE emails = ?”;
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, ‘s’, $email);
mysqli_stmt_execute($stmt);
//$result = mysqli_stmt_get_result($stmt); //Which line to use ? This line or the next ?
$result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // Which line to use ? This line or the one above ?
}
else
{
$username = $username_or_email;
$email = “”;
$query = “SELECT ids, usernames, passwords, emails, accounts_activations_statuses FROM users WHERE usernames = ?”;
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, ‘s’, $username);
mysqli_stmt_execute($stmt);
//$result = mysqli_stmt_get_result($stmt); //Which line to use ? This line or the next ?
$result = mysqli_stmt_bind_result($stmt, $db_id, $db_username, $db_password, $db_email, $db_account_activation_status); // Which line to use ? This line or the one above ?
}

$row = mysqli_stmt_fetch($stmt); //Which line to use ? This line or 2 of the next 2 ?
mysqli_stmt_close($stmt);
printf(“%s (%s)n”,$row[“usernames”],$row[“passwords”]);

if ($result == false)
{
echo “No result!”;// For debugging purpose!
exit();
}
elseif ($row[‘accounts_activations_statuses’] == ‘0’)
{
{
echo “You have not activated your account yet! Check your email for instructions on how to activate it.
Check your spam folder if you don’t find an email from us.”;
exit();
}
}
else
{

if (password_verify($password, $db_password))
{
echo “IF triggered for password_verify! password_verify ok”; // For debugging purpose!

$_SESSION[“user”] = $username;
header(“location:home.php?user=$username”);
}
else
{
echo “Incorrect User Credentials !’;<br>”;
exit();
}
}
}

?>

<!DOCTYPE html>
<html>
<head>
<title><?php $site_name?> Member Login Page</title>
<meta charset=”utf-8″>
</head>
<body>
<form method=”post” action=””>
<h3><?= $site_name ?> Member Login Form</h3>
<fieldset>
<label for=”login_name”>Username/Email:</label>
<input type=”text” name=”login_username_or_email” id=”login_name” value=””>
<br>
<label for=”login_pass”>Password:</label>
<input type=”password” name=”login_password” id=”login_pass” value=””>
</fieldset>
<div class=”submitsAndHiddens”>
<label for=”login_remember”>Remember Login Details:</label>
<input type=”checkbox” name=”login_remember” />
<br>
<button type=”submit”>Login</button>
<br>
<a href=”login_password_reset.php”>Forgot your Password ? Reset it here!</a>
<br>
<a href=”register.php”>Register here!</a>
</div>
</form>

</body>
</html>

[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@uniqueideamanauthorOct 01.2017 — Folks,

Do you mind reviewing my code in my original post and edit wherever you deem editing is necessary ?

I prefer 2 versions where on one you use the mysqli_stmt_get_result and on the other mysqli_stmt_bind_result.

That way, we newbies learn both from you with your examples.
Copy linkTweet thisAlerts:
@NogDogOct 02.2017 — FWIW, I never use bind_result(). Not saying that's a good argument one way or the other. *shrug*
Copy linkTweet thisAlerts:
@uniqueideamanauthorOct 02.2017 — FWIW, I never use bind_result(). Not saying that's a good argument one way or the other. *shrug*[/QUOTE]

Ok. So, how-about a code sample so we can see how you'd actually code it ? Learn your style of coding. ?

You can always open a mini tutorial, when you got the time. ?

Thanks!
Copy linkTweet thisAlerts:
@NogDogOct 02.2017 — Well, simply looking at the manual page for mysql_stmt_bind_result() you have a pretty good example. Here it is with the only change being I added a bound input parameter to the query:
[code=php]<?php
$code = 'test value';

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (!$link) {
printf("Connect failed: %sn", mysqli_connect_error());
exit();
}

/* prepare statement */
if ($stmt = mysqli_prepare($link, "SELECT Code, Name FROM Country WHERE Code = ?")) {
/* bind the test input value to the query place-holder */
mysqli_stmt_bind_param($stmt, 's', $code);

mysqli_stmt_execute($stmt);

/* bind variables to prepared statement */
mysqli_stmt_bind_result($stmt, $col1, $col2);

/* fetch values */
while (mysqli_stmt_fetch($stmt)) {
printf("%s %sn", $col1, $col2);
}

/* close statement */
mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($link);
[/code]
×

Success!

Help @uniqueideaman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...