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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c