将PHP代码从MySql转换为Sqlsrv时遇到代码问题

时间:2017-03-29 18:11:04

标签: php mysql sqlsrv

我已经从MySql迁移到Sqlsrv并且正在更新我的PHP。我不是那么精通PHP而且绝对不是Sqlsrv,而是使用PHP:SQLSRV手册。 这是我的PHP代码:

<?php
include '../includes/dbconfig.php';
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {

// username and password sent from Form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
//extract($_POST);

$sql="SELECT username FROM dbname WHERE username='$myusername' and userpassword='$mypassword'";
$result=sqlsrv_query($conn,$sql) or die(print_r(sqlsrv_errors(),true));

if ($result) {
   $rows = sqlsrv_has_rows( $result );
   if ($rows === true)
        echo "There are rows. <br />";
    {
        $_SESSION['login_user']=$myusername;
        header("Location: orderform.php");
    }
   else 
    { $error="Your Login Name or Password is invalid"; }
}
}
?>

导致问题的代码是:

{
$_SESSION['login_user']=$myusername;
header("Location: orderform.php");
}

如果我对这些行进行评论,则会出现登录表单,如果我输入了正确的用户名和密码,我会收到“有行”评论。如果没有,我会收到“您的登录名或密码无效”。一旦我将这些线重新放入,登录屏幕就不会出现。我没有得到任何错误或错误。我已经工作了2天......有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我认为你的支架结构错误了:

   if ($rows === true)
        echo "There are rows. <br />";
    {
        $_SESSION['login_user']=$myusername;
        header("Location: orderform.php");
    }
   else 
    { $error="Your Login Name or Password is invalid"; }

应该是:

   if ($rows === true)
    {
        echo "There are rows. <br />";
        $_SESSION['login_user']=$myusername;
        header("Location: orderform.php");
    }
   else 
    { $error="Your Login Name or Password is invalid"; }