0

I use the below code to get check the log in details using php. But I can't get the result row count. The same query executed in mysql workbench and return a row.But here it's not working. Please help me to do this. Because am new for php.

<?php
include("config.php");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from Form
$myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['password']);
echo "$myusername<br />";
echo "$mypassword<br />";
$sql="SELECT * FROM TBL_ADMIN_LOGIN WHERE FLD_USER='$myusername' and FLD_PASS='$mypassword'";

$result=mysql_query($sql);
$row=mysql_fetch_array($result);    
$active=$row['active'];
$count=mysql_num_rows($result);
echo $count;
//echo "$result[0]<br />";
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
session_register("myusername");
$_SESSION['login_user']=$myusername;

header("location: welcome.php");
}
else
{
$error="Your Login Name or Password is invalid";
}
}
?>
6
  • try if($count> 0) instead if($count==1) Jan 16, 2014 at 6:59
  • mysql_* and session_register = Deprecated Jan 16, 2014 at 7:01
  • echo $sql; and run this output in your mysql server and let us know how many rows return? Jan 16, 2014 at 7:02
  • echo $count; always return zero Jan 16, 2014 at 7:04
  • @AwladLiton it return a row Jan 16, 2014 at 7:04

1 Answer 1

0

Try This

$result=mysql_query($sql);
$count=mysql_num_rows($result);
$row=mysql_fetch_array($result);    
$active=$row['active'];

Or Use This Query

$sql="SELECT COUNT(*) AS `num` FROM TBL_ADMIN_LOGIN WHERE FLD_USER='$myusername' and 
FLD_PASS='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);    
$count=$row['num'];

Or

$sql="SELECT * FROM TBL_ADMIN_LOGIN WHERE FLD_USER='$myusername' and FLD_PASS='$mypassword'";
$result=mysql_query($sql);
if($row=mysql_fetch_array($result)){
  $active=$row['active'];
  $_SESSION['login_user']=$myusername;
  header("location: welcome.php");
}else{$error="Your Login Name or Password is invalid";}

And don't use session_register

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.