sqlsrv_fetch_array()期望参数1是资源

时间:2013-06-17 09:18:13

标签: php sql-server-2008

总而言之,我正在开展一个包含的项目。

MSSQL server 2008,ISS 7,PHP

我想使用$ _SESSION但是当我使用它时,它会出现在ISS7的记录中

PHP Notice:  Undefined index: Username in line 14
PHP Notice:  Undefined index: Afdeling in line 15
PHP Notice:  Undefined index: Mail in line 16

以下是这些错误的脚本:

session_start();
include_once("connect.php");

if (isset($_POST['username'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $tsql = "SELECT id, username, afdeling, mail FROM dbo.login WHERE         username='" . $username . "' AND password='" . $password . "'";
    $res = sqlsrv_query($conn, $tsql);
    $row = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC);
    if ($row) {
        $_SESSION['id'] = $row['id'];
        $_SESSION['username'] = $row['Username'];
        $_SESSION['afdeling'] = $row['Afdeling'];
        $_SESSION['mail'] = $row['Mail'];
        header("Location: calender.php");
    } else {
        echo "login information is incorrect please try again.";
    }
}

页面connect.php包含:

$serverName = "TEST"; //serverName\instanceName
$connectionInfo = array("Database" => "TEST", "UID" => "bla", "PWD" => "bloo");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn) {
    echo "<span style='color:green;'>Connection established.</span><br />";
} else {
    echo "<span style='color:red;'>Connection could not be established.</span><br />";
    die(print_r(sqlsrv_errors(), true));
}
?>

包含post函数的脚本:

echo "<form class='Login' action='TF.php' method='post'></br>
Username: <input type='text' name='username' STYLE='width:200px';/>&nbsp;<br />
Password:&nbsp;  <input type='password' name='password' STYLE='width:200px;'     />&nbsp;<br />
<input class='loginbutton' type='submit' name='submit' value='Log In' />

我该如何解决这些错误? 非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

print_r($row);来电后立即sqlsrv_fetch_array()检查$row包含的内容。

我猜您的数组索引存在区分大小写问题($row['Username']$row['username']$row['USERNAME']不一致。)

答案 1 :(得分:0)

您的查询中包含小写名称,但行中的第一个字母为大写:

查询:

SELECT id, username, afdeling, mail 

$ row:

$_SESSION['username'] = $row['Username'];
$_SESSION['afdeling'] = $row['Afdeling'];
$_SESSION['mail'] = $row['Mail'];

所以这应该是:

$_SESSION['username'] = $row['username'];
$_SESSION['afdeling'] = $row['afdeling'];
$_SESSION['mail'] = $row['mail'];