连接的sqlserver错误

时间:2013-05-22 13:58:06

标签: php sql-server login dreamweaver

好的,这是我的脚本尝试连接到sqlserver使用以前存储在数据库中的凭据登录但是当我尝试访问该页面时,我收到了错误

“解析错误:语法错误,第32行的login.php中出现意外的T_ELSE”

我查了各种网站,似乎无法找出这个脚本的错误

先谢谢。

  <?php
        session_start();

        if(!isset($_SESSION["user_id"])){
            header("location");
        }

    ?>
    <?php
        $username = $_POST['txt_username'];
        $password = $_POST['txt_password'];
    if ($username&&$password){  

        $connect = mysql_connect("database", "username", "password") or die("No Server Found");

        mysql_select_db("hnd_1213_marwick") or die("No Connection");

        $query = mysql_query("SELECT * FROM account WHERE username='$username'");
        $numrows = mysql_num_rows($query); 

        if($numrows !=0){
            while ($rows = mysql_fetch_assoc($query)){

                $dbusername = $row['username'];
                $dbpassword = $row['password'];
            }
            if ($username==$dbusername&&$password==dbpassword){

            echo "Login Successful. <a href='homepage.php'>Click Here to go to the home page</a>";
            $_SESSION['username']=$dbusername;

            else
            echo "Incorrect Login";


        else
        die ("This account does not exsist");

    }
    else
        die ("Please enter a username and password");

    ?>

2 个答案:

答案 0 :(得分:2)

你得到的错误与sql无关,它与php语法有关。

为了防止出现这类错误,请考虑使用 您的if else语句的{}。 还要确保您有缩进。 例如:

if( conditionA )
{
    if( conditionB )
    {

    }
    else
    {

    }
}
else
{

}

因此,您可以更轻松地跟踪那些缺失的大括号。

           if ($username==$dbusername&&$password==dbpassword){

            echo "Login Successful. <a href='homepage.php'>Click Here to go to the home page</a>";
            $_SESSION['username']=$dbusername;

            ///Add this brace below
}
            else
            echo "Incorrect Login";

答案 1 :(得分:0)

你错过了一个结束花括号

        if ($username==$dbusername&&$password==dbpassword){

        echo "Login Successful. <a href='homepage.php'>Click Here to go to the home page</a>";
        $_SESSION['username']=$dbusername;
        }// here is missing

这导致T_ELSE语法错误与php语法有关,而不是sql。在if statments

中使用花括号是非常有用的
if(something) { 
  //do stuff 
} else { 
  //do somthing else
}