无法使用Php从MySql数据库中选择数据

时间:2016-01-02 21:34:52

标签: php html mysql phpmyadmin

我在从phpMyAdmin创建的MySql数据库中选择数据时遇到问题。输出始终是:“无效的用户名”,即使我已经从名为st_login的数据库和phpMyAdmin中名为login的表中插入了我想要选择的数据。有人能告诉我错误吗?

<html >
<head>
<title></title>
</head>
<body>
<?php
print ("<form action='logincontroltest.php' method='post'>

    <p>Username
        <input type='text' name='username' />
    </p>
    <p>Password
        <input type='password' name='password' >
        <p/>
    <input type='submit' value='Log In'/>
</form>");
extract ($_POST);
if( !($database=mysql_connect("localhost","root",""))||!(mysql_select_db("st_login",$database))  )
    print("Could not connect");
if(isset($_POST['username'])&&isset($_POST['password']) )
{
    $username=$_POST['username'];
    $password=$_POST['password'];
    if ( !empty($username) &&!empty($password) ) 
    {
        $query = " SELECT 'id' FROM login WHERE   'username'='$username' AND 'password'='$password'  ";
        if($result=mysql_query($query,$database))
        {
            $query_num_rows=mysql_num_rows($result);
            if ($query_num_rows==0){
                echo "invalid username";}
            else if($query_num_rows==1) {
                echo "You logged in successfully";
            }
        } 
        die (mysql_error());
    }
    else echo "Fill in all blank fields";
} 
    ?>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

了解错误报告 - http://php.net/manual/en/function.mysql-error.php

而且,SQL查询中的单引号是否会破坏它?试着用这个:

$query = " SELECT id FROM login WHERE username='$username' AND password='$password' "