Php / MYSQL问题。 mysql_num_rows()返回0

时间:2012-12-07 02:10:30

标签: php mysql phpmyadmin

我正在尝试为我的网站创建一个登录系统。这就是我所拥有的。

    include('MYSQL_connect_userdata.php');
    $query = "SELECT * FROM userinfo WHERE username = '$username' AND password = '$password' ";
    $result = mysql_query($query) or die("cant find table");
    $count2 = mysql_num_rows($result);
    $resultarray = mysql_fetch_array($result);
    echo "SELECT * FROM userinfo WHERE username = '$username' AND password = '$password' ";

MYSQL_connect_userdata.php连接到mysql服务器并选择数据库。

当我粘贴输出时      echo“SELECT * FROM userinfo WHERE username ='$ username'AND password ='$ password'”;

phpmyadmin返回我要查找的行。 (包含用户名和密码“)

由于某种原因,即使输入值正确,mysql_num_rows($ result)也会返回0。在php文件的顶部使用$ _POST获取输入

$username = $_POST['username'];
$password = $_POST['password'];

如果我更改查询以排除“AND password ='$ password'”;部分然后页面按预期工作,mysql_num_rows返回1.

有什么想法吗?我是php的新手,所以额外的解释将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db",$con);

$sql = "SELECT * FROM person";


//Your missing the connection , store in a variable
$result = mysql_query($sql,$con);

echo mysql_num_rows($result);

mysql_close($con);