dscizpq790832708 2013-06-17 09:18
浏览 116
已采纳

sqlsrv_fetch_array()期望参数1是资源

To sum it all up, I am working on an project which includes.

MSSQL server 2008, ISS 7, PHP

I want to use $_SESSION but when I use it this appears in the logging of 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

Here is the script for those errors:

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.";
    }
}

And the page connect.php contains this:

$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));
}
?>

The script containing the post function:

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' />

how can i fix these errors? Any help is greatly appreciated.

  • 写回答

2条回答 默认 最新

  • duanhuan6336 2013-06-17 09:22
    关注

    Do a print_r($row); just after the sqlsrv_fetch_array() call to check what $row contains exactly.

    I guess you have a case sensitivity issue with your array indexes ($row['Username'] not being the same as $row['username'] or $row['USERNAME']).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)