PHP搜索查询错误:sqlsrv_fetch_array()期望参数1是资源,布尔

时间:2017-12-04 09:48:53

标签: php html server sqlsrv

我尝试使用文本框和按钮搜索我的数据库,然后将其显示在表格中,仍然开始,所以我对此非常震动。我一直收到这个错误:

警告:sqlsrv_fetch_array():2在第55行的C:\ RDEUsers \ NET \ 531545 \ LocationPOST.php中不是有效的ss_sqlsrv_stmt资源

对于究竟是什么错误非常困惑。

        <?php

if(isset($_POST['Search']))
{
    $SearchValue = $_POST['SearchValue'];
    // search in all table columns
    // using concat mysql function
    $LocationQuery = "SELECT * FROM Location WHERE (Username, First_Name, Surname, Current_Location) LIKE '%".$SearchValue."%'";
    $search_result = FilterTable($LocationQuery);

}
 else {
    $LocationQuery = "SELECT * FROM Location";
    $search_result = FilterTable($LocationQuery);
}

// function to connect and execute the query
function FilterTable($LocationQuery)
{
$server = 'SQL2008.net.dcs.hull.ac.uk';
$connectionInfo = array("Database"=>"rde_531545");
$conn = sqlsrv_connect($server,$connectionInfo);

$Filter_Result = sqlsrv_query($conn, $LocationQuery);
 return $Filter_Result;
}

?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title> Location Tracker </title>
</head>

<center><h1> ACW Location Tracker </h1></center>

<body>

 <form action="LocationPOST.php" method="POST">
<input type="text" name="SearchValue" placeholder="Username"><br><br>
<input type="submit" name="Search" value="Search"><br><br>


<table>
<tr>
<th> Username </th>
<th> First_Name </th>
<th> Surname </th>
<th> Current_Location </th>

</tr>

<?php while($row = sqlsrv_fetch_array($search_result)):?>

       <tr>
                    <td><?php echo $row['Username'];?></td>
                    <td><?php echo $row['First_Name'];?></td>
                    <td><?phpecho $row['Surname'];?></td>
                    <td><?php echo $row['Current_Location'];?></td>                 
       </tr>

<?php endwhile;?>

<?php



//set timezone to current timezone GMT
date_default_timezone_set('Europe/London');

$server = 'SQL2008.net.dcs.hull.ac.uk';
//sets up connection to database
$connectionInfo = array("Database"=>"rde_531545");
$conn = sqlsrv_connect($server,$connectionInfo);
//creates a table called location in the database
$LocationQuery='create table Location ';
$LocationQuery .= '(Username int NOT NULL IDENTITY(500, 23), First_Name varchar(50) NOT NULL, Surname varchar(50) NOT NULL, Current_Location varchar(50) NOT NULL, Date date NOT NULL, PRIMARY KEY (Username))';
$result = sqlsrv_query($conn, $LocationQuery);

if (!$result)
{
 if( ($errors = sqlsrv_errors() ) != null)
{
foreach( $errors as $error )
{
echo "<p>Error: ".$error[ 'message']."</p>";
}
}
}
else
{
echo "<p>DB successfully created</p>";
}
//close server connection
sqlsrv_close($conn);

$server = 'SQL2008.net.dcs.hull.ac.uk';
$connectionInfo = array( "Database"=>"rde_531545");
$conn = sqlsrv_connect($server,$connectionInfo);
date_default_timezone_set('Europe/London');
$date =  new DateTime('');
$dateStr = $date->format("Y-m-d H:i:s");
$d=strtotime("");
$insert_query = "INSERT INTO Location (First_Name, Surname, Current_Location) VALUES (?, ?, ?, ?)";
$params = array("John","Doe","Hull" );
$result = sqlsrv_query($conn,$insert_query,$params);
$params = array("Jane","Doe","London");
$result = sqlsrv_query($conn,$insert_query,$params);
$params = array("Jon","Doe","Sheffield");
$result = sqlsrv_query($conn,$insert_query,$params);

//select all data from table Location in the database
$LocationQuery='SELECT * FROM Location';
$results = sqlsrv_query($conn, $LocationQuery);
// while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
// {
// echo '<p>'.$row['Username'].' '.$row['First_Name'].' '.$row['Surname'].' '.$row['Current_Location']. '</p>';
// }
// sqlsrv_close($conn);

//display the associative array results in a table
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
    echo "<tr><td>".$row['Username']."</td><td>".$row['First_Name']."</td><td>".$row['Surname']."</td><td>".$row['Current_Location']."</td><td>".$dateStr = $date->format("Y-m-d H:i:s")."</td></tr>";  
}

sqlsrv_close($conn);
?>

</table>

</form>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的一个查询失败了(我猜是"SELECT * FROM Location WHERE (Username, First_Name, Surname, Current_Location) LIKE '%".$SearchValue."%'")。

如果sqlsrv_query失败,则返回false(您不检查)并将其传递给sqlsrv_fetch_array