Php单个查询总是返回o count

时间:2014-01-16 06:58:55

标签: php mysql

我使用下面的代码来使用php检查登录详细信息。但我无法得到结果行数。在mysql工作台中执行相同的查询并返回一行。但这里它不起作用。请帮我这样做。因为我是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";
}
}
?>

1 个答案:

答案 0 :(得分:0)

试试这个

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

或使用此查询

$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'];

或者

$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";}

不要使用session_register