When the login starts, it does not show me the data that I want to show, in this case the account_id . How can I solve it?
$account_email = $_POST['account_email'];
$account_password = $_POST['account_password'];
$account = 0;
$login_get = "SELECT COUNT(*) FROM $users WHERE account_email = ? AND account_password = ?";
$login_result = mysqli_prepare($base, $login_get);
mysqli_stmt_bind_param($login_result, "ss", $account_email, $account_password);
mysqli_stmt_execute($login_result);
mysqli_stmt_bind_result($login_result, $account);
mysqli_stmt_fetch($login_result);
if ($account > 0) {
while (mysqli_stmt_fetch($list)) {
echo $account_id;
}
mysqli_stmt_close($login_result);
}
In my answer to another question of yours: How do I check if the account exists in the database, I told you that the best way to know if a record exists is by counting .
Now, if what interests you is something else, (for this specific case to obtain the column
account_id
) you would have to select that specific column.I propose this solution, improving the code a bit and correcting other errors that I will indicate at the end:
Some considerations
account_id
). In addition to that, I have intuitively addedLIMIT 1
, assuming that this query is looking for a single row. If I am wrong in that intuition, tell me and I will modify the answer. All this is looking for is to optimize the code .while
to read the results, because it is intuited that a single row is expected and in those cases it is not necessarywhile
. It is important that you understand that what it doesmysqli_stmt_fetch
is move the result pointer and that there are two ways to do this: If more than one row is expected, you apply it viawhile
; if a single row is expected, you apply it directly. Your code is poorly implemented in this regard, because you usemysqli_stmt_fetch
with and withoutwhile
. In that case, if there were multiple rows, the partwhile
would start reading you from the second row onwards, because the firstmysqli_stmt_fetch
already moved the pointer once (1) .$login_result
to a result withmysqli_stmt_bind_result
. The purpose of that method is to bind each column returned by the SQL query to variables. For example, this query expects a column that would beaccount_id
and we would be binding it to the variable$account
. If more columns are expected, you must bind them to variables intended for it . Your variable$login_result
represents the prepared query, you can't also use it to bind the result of one of the columns . I suggest you read the documentation to avoid this type of error that reveals ignorance about how prepared queries work in mysqli.If there is something that is not clear, ask in comments, below this answer.
Grades:
(1). For more details on this you can check here and also here .