-2

我刚开始使用 PHP。我目前正在为我的网页编写登录脚本。当我尝试运行mysql_query(SELECT * FROM users WHERE username=$username) 时,它不起作用。我确保我使用了正确的名称,但我一直遇到查询功能的问题。这是我的代码,有人看到问题吗?

<?php

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

if ($username&&$password)
{
    $connect = mysql_connect("host", "dbname", "password!") or die ("Couldnt connect!");
    mysql_select_db("TwoogLogin") or die ('couldnt find datebase');

    $query = mysql_query("SELECT * FROM users WHERE username=$username") or die ('unable to run query');

    $numrows = mysql_num_rows($query);

    echo $numrows;
}
else
    die("Please enter a username and a password");
?>
4

3 回答 3

1

试试这个方法:

<?php
$username = $_POST['username'];
$password = $_POST['password'];

if ($username!=='' && $password!==''){
$connect = mysql_connect("host", "dbname", "password!");
if (!$connect)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("TwoogLogin",$connect);
if (!$db_selected)
  {
  die ("Cannot use TwoogLogin: " . mysql_error());
  }

$query = "SELECT * FROM users WHERE username='$username'";
$numrows = mysql_query($query,$connect);
echo mysql_num_rows($numrows);

mysql_close($connect);
}else{
echo "Please enter a username and a password";
}
?>

正如下面评论中所建议的那样 (@Pascamel)msql_*扩展已被弃用PHP 5.5.0,并将在未来被删除。相反,应该使用MySQLior扩展名。PDO_MySQL

<?php
$username = $_POST['username'];
$password = $_POST['password'];


if ($username!=='' && $password!==''){
$connect = mysqli_connect("host", "dbname", "password!");
if (!$connect)
  {
  die('Could not connect: ' . mysqli_error());
  }

$db_selected = mysqli_select_db("TwoogLogin",$connect);

$query = "SELECT * FROM users WHERE username='$username'";
$numrows = mysqli_query($query,$connect);
echo mysqli_num_rows($numrows);

mysqli_close($connect);
}else{
echo "Please enter a username and a password";
}
?>
于 2013-09-01T15:47:52.547 回答
0
<?php
//Set appropriate value for the following statements
$connect = mysql_connect("mysql_server", "mysql_user", "mysql_password") or die ("Couldnt connect!");
mysql_select_db("database_name",$connect) or die ('couldnt find datebase');


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

if ($username && $password)
{
    $query = mysql_query("SELECT * FROM users WHERE username='$username'") or die ('unable to run query');

    $numrows = mysql_num_rows($query);

    echo $numrows;
}
else
    die("Please enter a username and a password");
?>
于 2013-09-01T15:39:13.170 回答
-1

查询中的语法错误!你应该使用 username='$username' 另一个原因是关于列名不好你确定你的列名是正确的吗?

于 2013-09-01T15:21:27.580 回答