Click here to Skip to main content
15,617,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
$error = "username/password incorrect";

//php cheacking To login
							$message = "";
								$message = $message."Error:<br/> Please Enter User Name";
							}else {
								$error = 0;
								$message = $message."<br/> Please Enter Password";
							}else {
								$error = 0;
							if($_POST["user_type"]=="Select User Type"){
								$message = $message."<br/> Please select user type";
							}else {
								$error = 0;
//database related

$sql= "SELECT * FROM users WHERE username='$username' and password='$password' and user_type='$user_type'";
// $stmt=$connection->prepare($query);
// $stmt->bind_param("sss",$username,$password,$user_type);
// $stmt->execute();
// $result=$stmt->get_result();
// $row=$result->fetch_assoc();
// session_regenerate_id();
// $_SESSION['username']=$row['username'];
// $_SESSION['user_type']=$row['user_type'];
// $_SESSION['password']=$row['password'];
// session_write_close();
// if ($result->num_rows==1 && $_SESSION['user_type']=="student") {
// 	header("location:student/home.php");
// }
// else if ($result->num_rows==1 && $_SESSION['user_type']=="Department") {
// 	header("location:Department/home.php");
// }
// else if ($result->num_rows==1 && $_SESSION['user_type']=="manager") {
// 	header("location:manager/home.php");
// }
// else if ($result->num_rows==1 && $_SESSION['user_type']=="Registrar") {
// 	header("location:Classes/home.php");
// }
$qr = mysqli_query($connection,$sql);
$result = mysqli_num_rows($qr);
									if($_SESSION["user_type"]=="manager" and isset($_SESSION["username"])  and isset($_SESSION["dept"])){
									header("Location: manager/home.php");
									else if($_SESSION["user_type"]=="Department" and isset($_SESSION["username"])  and isset($_SESSION["dept"])){
									header("Location: Department/home.php");
									else if($_SESSION["user_type"]=="student" and isset($_SESSION["username"])  and isset($_SESSION["dept"])){
									header("Location: student/home.php");
									else if($_SESSION["user_type"]=="Registrar" and isset($_SESSION["username"])  and isset($_SESSION["dept"])){
									header("Location: Classes/home.php");

								echo "<div class='error'>"."Error".$message."</div>";

								echo "<div class='error'>"."Error".$message."</div>";

What I have tried:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\Cost Sharing\code.php on line 55
Updated 2-Aug-21 16:19pm

1 solution

If the query fails then the result from mysqli_query is a boolean (false). So first investigate if the query has failed, and if it hasn't, then check the number of rows. Consider the following example
if ($qr = mysqli_query($connection,$sql)) {
   $result = mysqli_num_rows($qr);

One likely reason for your query to fail is that you concatenate values directly to the SQL statement. This leaves you open to SQL injection and introduces different kinds of problems, see SQL injection - Wikipedia[^]

So try using parameters instead, for more information, see PHP: mysqli::prepare - Manual[^]

One more observation, you seem to store the password as plain text. This should never be done. To properly handle passwords, see Password Storage: How to do it.[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900