1

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

    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 很陌生,因此将不胜感激。谢谢。

4

1 回答 1

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);
于 2012-12-07T02:26:51.303 回答