Innominatum Innominatum - 1 year ago 116
PHP Question

sqlsrv_fetch_array() expects parameter 1 to be resource

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:


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.

Answer Source

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']).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download