Trouble using mysqli_stmt_bind_result











up vote
1
down vote

favorite












I am trying to use mysqli_stmt_bind_result() but I think I am not using it correctly. The code below grabs the username and password and runs a query in the database and if it matches it is meant to echo back success.



However, when I print out the row it is empty. What might I be doing wrong?



I am not planing to use mysqli_stmt_get_result() I know how that works. I want to make use of mysqli_stmt_bind_result in this case.



<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
require 'dbh.inc.php';
$email = $_POST['email'];
$pass = $_POST['pass'];

if (empty($email) || empty($pass)) {
header("Location: ../login.php?error=emptyfields");
exit();
}
else {
$sql = "SELECT idUsers, emailUsers, firstNameUsers, lastNameUsers, pwdUsers, bdayUsers FROM users WHERE emailUsers = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo 'errorme';
exit();
}
else {
mysqli_stmt_bind_param($stmt ,"s", $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id_Users ,$email_Users, $first_NameUsers, $last_NameUsers, $pass_word, $bday_Users);
if ($row=mysqli_stmt_fetch($stmt)) {
$message = $row["$email"];
echo "<script type='text/javascript'>alert('$message');</script>";
$pwdCheck = password_verify($pass, $row['pwdUsers']);
if ($pwdCheck == false) {
echo 'wrongpass';
exit();
}
else if($pwdCheck == true) {
session_start();
$_SESSION['userId'] = $row['idUsers'];
$_SESSION['userName'] = $row['firstNameUsers'];
echo 'success';
exit();
}
else {
echo 'noresults';
exit();
}
}
else {
echo 'nouser';
exit();
}
}
}
}
else {
header("Location: ../login.php");
exit();
}
?>









share|improve this question
























  • You should really not be using that ancient procedural mysqli stuff for new code. mysqli has an object-oriented interface that is slightly more modern, though many (myself included) would recommend using PDO instead.
    – miken32
    Nov 7 at 18:24










  • Also please verify what output you're getting from your script with the code provided, e.g. "nouser", "noresults", etc.
    – miken32
    Nov 7 at 18:26










  • You made use of bind_result, yet, you are not using all the bound variables in your script. See example 2 in the documentation
    – Ibu
    Nov 7 at 18:28












  • I am getting wrongpass...even though the pass is 100% correct (this might be due to the row being empty..). I will take a look at PDO.
    – Jack
    Nov 7 at 18:28












  • Use these variables that you defined $id_Users ,$email_Users, $first_NameUsers,... Not $row
    – Ibu
    Nov 7 at 18:30















up vote
1
down vote

favorite












I am trying to use mysqli_stmt_bind_result() but I think I am not using it correctly. The code below grabs the username and password and runs a query in the database and if it matches it is meant to echo back success.



However, when I print out the row it is empty. What might I be doing wrong?



I am not planing to use mysqli_stmt_get_result() I know how that works. I want to make use of mysqli_stmt_bind_result in this case.



<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
require 'dbh.inc.php';
$email = $_POST['email'];
$pass = $_POST['pass'];

if (empty($email) || empty($pass)) {
header("Location: ../login.php?error=emptyfields");
exit();
}
else {
$sql = "SELECT idUsers, emailUsers, firstNameUsers, lastNameUsers, pwdUsers, bdayUsers FROM users WHERE emailUsers = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo 'errorme';
exit();
}
else {
mysqli_stmt_bind_param($stmt ,"s", $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id_Users ,$email_Users, $first_NameUsers, $last_NameUsers, $pass_word, $bday_Users);
if ($row=mysqli_stmt_fetch($stmt)) {
$message = $row["$email"];
echo "<script type='text/javascript'>alert('$message');</script>";
$pwdCheck = password_verify($pass, $row['pwdUsers']);
if ($pwdCheck == false) {
echo 'wrongpass';
exit();
}
else if($pwdCheck == true) {
session_start();
$_SESSION['userId'] = $row['idUsers'];
$_SESSION['userName'] = $row['firstNameUsers'];
echo 'success';
exit();
}
else {
echo 'noresults';
exit();
}
}
else {
echo 'nouser';
exit();
}
}
}
}
else {
header("Location: ../login.php");
exit();
}
?>









share|improve this question
























  • You should really not be using that ancient procedural mysqli stuff for new code. mysqli has an object-oriented interface that is slightly more modern, though many (myself included) would recommend using PDO instead.
    – miken32
    Nov 7 at 18:24










  • Also please verify what output you're getting from your script with the code provided, e.g. "nouser", "noresults", etc.
    – miken32
    Nov 7 at 18:26










  • You made use of bind_result, yet, you are not using all the bound variables in your script. See example 2 in the documentation
    – Ibu
    Nov 7 at 18:28












  • I am getting wrongpass...even though the pass is 100% correct (this might be due to the row being empty..). I will take a look at PDO.
    – Jack
    Nov 7 at 18:28












  • Use these variables that you defined $id_Users ,$email_Users, $first_NameUsers,... Not $row
    – Ibu
    Nov 7 at 18:30













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to use mysqli_stmt_bind_result() but I think I am not using it correctly. The code below grabs the username and password and runs a query in the database and if it matches it is meant to echo back success.



However, when I print out the row it is empty. What might I be doing wrong?



I am not planing to use mysqli_stmt_get_result() I know how that works. I want to make use of mysqli_stmt_bind_result in this case.



<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
require 'dbh.inc.php';
$email = $_POST['email'];
$pass = $_POST['pass'];

if (empty($email) || empty($pass)) {
header("Location: ../login.php?error=emptyfields");
exit();
}
else {
$sql = "SELECT idUsers, emailUsers, firstNameUsers, lastNameUsers, pwdUsers, bdayUsers FROM users WHERE emailUsers = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo 'errorme';
exit();
}
else {
mysqli_stmt_bind_param($stmt ,"s", $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id_Users ,$email_Users, $first_NameUsers, $last_NameUsers, $pass_word, $bday_Users);
if ($row=mysqli_stmt_fetch($stmt)) {
$message = $row["$email"];
echo "<script type='text/javascript'>alert('$message');</script>";
$pwdCheck = password_verify($pass, $row['pwdUsers']);
if ($pwdCheck == false) {
echo 'wrongpass';
exit();
}
else if($pwdCheck == true) {
session_start();
$_SESSION['userId'] = $row['idUsers'];
$_SESSION['userName'] = $row['firstNameUsers'];
echo 'success';
exit();
}
else {
echo 'noresults';
exit();
}
}
else {
echo 'nouser';
exit();
}
}
}
}
else {
header("Location: ../login.php");
exit();
}
?>









share|improve this question















I am trying to use mysqli_stmt_bind_result() but I think I am not using it correctly. The code below grabs the username and password and runs a query in the database and if it matches it is meant to echo back success.



However, when I print out the row it is empty. What might I be doing wrong?



I am not planing to use mysqli_stmt_get_result() I know how that works. I want to make use of mysqli_stmt_bind_result in this case.



<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
require 'dbh.inc.php';
$email = $_POST['email'];
$pass = $_POST['pass'];

if (empty($email) || empty($pass)) {
header("Location: ../login.php?error=emptyfields");
exit();
}
else {
$sql = "SELECT idUsers, emailUsers, firstNameUsers, lastNameUsers, pwdUsers, bdayUsers FROM users WHERE emailUsers = ?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo 'errorme';
exit();
}
else {
mysqli_stmt_bind_param($stmt ,"s", $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id_Users ,$email_Users, $first_NameUsers, $last_NameUsers, $pass_word, $bday_Users);
if ($row=mysqli_stmt_fetch($stmt)) {
$message = $row["$email"];
echo "<script type='text/javascript'>alert('$message');</script>";
$pwdCheck = password_verify($pass, $row['pwdUsers']);
if ($pwdCheck == false) {
echo 'wrongpass';
exit();
}
else if($pwdCheck == true) {
session_start();
$_SESSION['userId'] = $row['idUsers'];
$_SESSION['userName'] = $row['firstNameUsers'];
echo 'success';
exit();
}
else {
echo 'noresults';
exit();
}
}
else {
echo 'nouser';
exit();
}
}
}
}
else {
header("Location: ../login.php");
exit();
}
?>






php mysql mysqli






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 18:36









Qirel

9,53862337




9,53862337










asked Nov 7 at 18:21









Jack

127114




127114












  • You should really not be using that ancient procedural mysqli stuff for new code. mysqli has an object-oriented interface that is slightly more modern, though many (myself included) would recommend using PDO instead.
    – miken32
    Nov 7 at 18:24










  • Also please verify what output you're getting from your script with the code provided, e.g. "nouser", "noresults", etc.
    – miken32
    Nov 7 at 18:26










  • You made use of bind_result, yet, you are not using all the bound variables in your script. See example 2 in the documentation
    – Ibu
    Nov 7 at 18:28












  • I am getting wrongpass...even though the pass is 100% correct (this might be due to the row being empty..). I will take a look at PDO.
    – Jack
    Nov 7 at 18:28












  • Use these variables that you defined $id_Users ,$email_Users, $first_NameUsers,... Not $row
    – Ibu
    Nov 7 at 18:30


















  • You should really not be using that ancient procedural mysqli stuff for new code. mysqli has an object-oriented interface that is slightly more modern, though many (myself included) would recommend using PDO instead.
    – miken32
    Nov 7 at 18:24










  • Also please verify what output you're getting from your script with the code provided, e.g. "nouser", "noresults", etc.
    – miken32
    Nov 7 at 18:26










  • You made use of bind_result, yet, you are not using all the bound variables in your script. See example 2 in the documentation
    – Ibu
    Nov 7 at 18:28












  • I am getting wrongpass...even though the pass is 100% correct (this might be due to the row being empty..). I will take a look at PDO.
    – Jack
    Nov 7 at 18:28












  • Use these variables that you defined $id_Users ,$email_Users, $first_NameUsers,... Not $row
    – Ibu
    Nov 7 at 18:30
















You should really not be using that ancient procedural mysqli stuff for new code. mysqli has an object-oriented interface that is slightly more modern, though many (myself included) would recommend using PDO instead.
– miken32
Nov 7 at 18:24




You should really not be using that ancient procedural mysqli stuff for new code. mysqli has an object-oriented interface that is slightly more modern, though many (myself included) would recommend using PDO instead.
– miken32
Nov 7 at 18:24












Also please verify what output you're getting from your script with the code provided, e.g. "nouser", "noresults", etc.
– miken32
Nov 7 at 18:26




Also please verify what output you're getting from your script with the code provided, e.g. "nouser", "noresults", etc.
– miken32
Nov 7 at 18:26












You made use of bind_result, yet, you are not using all the bound variables in your script. See example 2 in the documentation
– Ibu
Nov 7 at 18:28






You made use of bind_result, yet, you are not using all the bound variables in your script. See example 2 in the documentation
– Ibu
Nov 7 at 18:28














I am getting wrongpass...even though the pass is 100% correct (this might be due to the row being empty..). I will take a look at PDO.
– Jack
Nov 7 at 18:28






I am getting wrongpass...even though the pass is 100% correct (this might be due to the row being empty..). I will take a look at PDO.
– Jack
Nov 7 at 18:28














Use these variables that you defined $id_Users ,$email_Users, $first_NameUsers,... Not $row
– Ibu
Nov 7 at 18:30




Use these variables that you defined $id_Users ,$email_Users, $first_NameUsers,... Not $row
– Ibu
Nov 7 at 18:30












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You bind the results through mysqli_stmt_bind_result(), and these are the variables which will hold the values once your results are fetched.



Your fetch() function returns a boolean, and not a result (unlike the non-prepared queries).



if (mysqli_stmt_fetch($stmt)) {
$message = $email_Users;
echo "<script type='text/javascript'>alert('$message');</script>";
$pwdCheck = password_verify($pass, $pass_word);


The same goes for any place you attempt to use $row - you have to use the variables that are assigned in mysqli_stmt_bind_result() (so $id_Users instead of $row['idUsers'], $first_NameUsers instead of $row['firstNameUsers'] and so on).



See the documentation for each method for more details.




  • mysqli_stmt::bind_result()

  • mysqli_stmt::fetch()






share|improve this answer





















  • Perfect makes complete sense thank you so much!
    – Jack
    Nov 7 at 18:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195500%2ftrouble-using-mysqli-stmt-bind-result%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










You bind the results through mysqli_stmt_bind_result(), and these are the variables which will hold the values once your results are fetched.



Your fetch() function returns a boolean, and not a result (unlike the non-prepared queries).



if (mysqli_stmt_fetch($stmt)) {
$message = $email_Users;
echo "<script type='text/javascript'>alert('$message');</script>";
$pwdCheck = password_verify($pass, $pass_word);


The same goes for any place you attempt to use $row - you have to use the variables that are assigned in mysqli_stmt_bind_result() (so $id_Users instead of $row['idUsers'], $first_NameUsers instead of $row['firstNameUsers'] and so on).



See the documentation for each method for more details.




  • mysqli_stmt::bind_result()

  • mysqli_stmt::fetch()






share|improve this answer





















  • Perfect makes complete sense thank you so much!
    – Jack
    Nov 7 at 18:40















up vote
2
down vote



accepted










You bind the results through mysqli_stmt_bind_result(), and these are the variables which will hold the values once your results are fetched.



Your fetch() function returns a boolean, and not a result (unlike the non-prepared queries).



if (mysqli_stmt_fetch($stmt)) {
$message = $email_Users;
echo "<script type='text/javascript'>alert('$message');</script>";
$pwdCheck = password_verify($pass, $pass_word);


The same goes for any place you attempt to use $row - you have to use the variables that are assigned in mysqli_stmt_bind_result() (so $id_Users instead of $row['idUsers'], $first_NameUsers instead of $row['firstNameUsers'] and so on).



See the documentation for each method for more details.




  • mysqli_stmt::bind_result()

  • mysqli_stmt::fetch()






share|improve this answer





















  • Perfect makes complete sense thank you so much!
    – Jack
    Nov 7 at 18:40













up vote
2
down vote



accepted







up vote
2
down vote



accepted






You bind the results through mysqli_stmt_bind_result(), and these are the variables which will hold the values once your results are fetched.



Your fetch() function returns a boolean, and not a result (unlike the non-prepared queries).



if (mysqli_stmt_fetch($stmt)) {
$message = $email_Users;
echo "<script type='text/javascript'>alert('$message');</script>";
$pwdCheck = password_verify($pass, $pass_word);


The same goes for any place you attempt to use $row - you have to use the variables that are assigned in mysqli_stmt_bind_result() (so $id_Users instead of $row['idUsers'], $first_NameUsers instead of $row['firstNameUsers'] and so on).



See the documentation for each method for more details.




  • mysqli_stmt::bind_result()

  • mysqli_stmt::fetch()






share|improve this answer












You bind the results through mysqli_stmt_bind_result(), and these are the variables which will hold the values once your results are fetched.



Your fetch() function returns a boolean, and not a result (unlike the non-prepared queries).



if (mysqli_stmt_fetch($stmt)) {
$message = $email_Users;
echo "<script type='text/javascript'>alert('$message');</script>";
$pwdCheck = password_verify($pass, $pass_word);


The same goes for any place you attempt to use $row - you have to use the variables that are assigned in mysqli_stmt_bind_result() (so $id_Users instead of $row['idUsers'], $first_NameUsers instead of $row['firstNameUsers'] and so on).



See the documentation for each method for more details.




  • mysqli_stmt::bind_result()

  • mysqli_stmt::fetch()







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 18:34









Qirel

9,53862337




9,53862337












  • Perfect makes complete sense thank you so much!
    – Jack
    Nov 7 at 18:40


















  • Perfect makes complete sense thank you so much!
    – Jack
    Nov 7 at 18:40
















Perfect makes complete sense thank you so much!
– Jack
Nov 7 at 18:40




Perfect makes complete sense thank you so much!
– Jack
Nov 7 at 18:40


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195500%2ftrouble-using-mysqli-stmt-bind-result%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()