I am creating a login script for my web app and am trying to use $count = mysqli_stmt_num_rows($stmt);
to find the number of rows returned from the sql select statement, so I can then decide if a session should be started.
The problem is, $count is always 0, even when I enter valid user name and password that matches the data in my database. I have tested the select statement, it works fine. No errors, syntax, SQL or otherwise are given, so i'm kinda stuck as to whats happening.
CODE:
<?php
$link = mysqli_connect("localhost", "****", "****", "****");
//check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// Move to MySQL(i) as MySQL is now obslete and use Prepare statment for protecting against SQL Injection in better and easier way
$stmt = mysqli_prepare($link, 'SELECT username, password FROM `users` WHERE `username` = ? AND `password` = ?');
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "ss", $myusername, $mypassword);
/* execute query */
mysqli_stmt_execute($stmt);
/*count number of rows returned*/
$count = mysqli_stmt_num_rows($stmt);
/*display number of rows returned*/
//echo $count;
/* bind result variables */
mysqli_stmt_bind_result($stmt, $myusername, $mypassword);
/* fetch value */
mysqli_stmt_fetch($stmt);
/* close statement */
mysqli_stmt_close($stmt);
if($count == 1) {
session_start();
$_SESSION['userid'] = $myusername;
header("location:index.php");
exit;
} else {
echo "Wrong Username or Password";
echo "<form name='form5' action='main_login.html'>";
echo "<input type='submit' name='Submit' value='Log-in'>";
echo "</form>";
}
/* close connection */
mysqli_close($link);
?>
mysqli_stmt_num_rows
documentation? PS: why don't you use sqlCOUNT
instead? – Men