Reputation: 11
I've searched high and low for this and I can't wrap my head around it. I have a form to search Permit Applications. Input your permit number and it runs 3 queries against 3 separate SQL Server views. The queries are almost identical in the same if statement but the last one fails. I've performed a var_dump($query_insp_history)
copied the output and run a new query directly in SSMS and it works just fine. Is there some other error checking that will give me more detail other than sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given
? I believe I have my parameters setup correctly, please let me know if it should be structured differently.
The // Application Inspection History Section
is the part giving me issues. I just can't figure it out as it appears it's setup the same way the other two queries are. I receive the error at this line of code while($row = sqlsrv_fetch_array($sql_insp_history, SQLSRV_FETCH_ASSOC)){
var_dump($sql_insp_history);
returns
bool(false)
var_dump($query_insp_history);
returnsstring(81) "SELECT * FROM my.dbo.vw_Permit_App_Insp_History WHERE (LTRIM(APNO) = '123456')"
Again, copying this into SSMS runs correctly...
Thank you in advance.
Here is my code.
<div class="container content">
<form method="POST" name="permit_search" action="">
<input type="number" id="apno" name="apno" class="col-sm-8">
<input type="submit" name="apnosearch" id="apnosearch" value="Submit" class="col-sm-4">
</form>
<br/>
<?php
error_reporting(E_ALL ^ E_NOTICE);
$serverName = "my_server";
$connectionInfo = array( "Database"=>"myDb", "UID"=>"myUser", "PWD"=>"myPass", "ReturnDatesAsStrings" => true);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if($conn === false) {
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
// Query variables
$searchapno = $_POST['apno'];
$where_permit_info = "WHERE (LTRIM(APNO) = '$searchapno')";
$where_review_history = "WHERE (LTRIM(APNO) = '$searchapno') ORDER BY ACTTYPE, TYPENO ASC";
$where_insp_history = "WHERE (LTRIM(APNO) = '$searchapno') ORDER BY INSPTYPE, TYPENO ASC";
$query_permit_info = "SELECT TOP 1 * FROM my.dbo.vw_Permit_Information $where_permit_info";
$query_review_history = "SELECT * FROM my.dbo.vw_Permit_Plan_Review_History $where_review_history";
$query_insp_history = "SELECT * FROM my.dbo.vw_Permit_App_Insp_History $where_insp_history";
// Overall Permit Information
if (isset($_POST['apno'])) {
$sql_permit_info = sqlsrv_query($conn, $query_permit_info);
while ($row= sqlsrv_fetch_array($sql_permit_info, SQLSRV_FETCH_ASSOC)) {
echo "<strong>Name:</strong> ".$row['APNAME']."<br>";
echo "<strong>Number:</strong> ".$row['APNO']."<br>";
echo "<strong>Address:</strong> ".$row['STNO']." ".$row['PREDIR']." ".$row['STNAME']." ".$row['SUFFIX']." ".$row['POSTDIR']."<br>";
echo "<strong>Description:</strong> ".$row['DESCRIPT']."<br>";
echo "<strong>Status:</strong> ".$row['STAT']."<br>";
echo "<h3>Application Stages</h3><br>";
echo "<strong>Date Processed:</strong> ".$row['ADDDTTM']."<br>";
echo "<strong>Date Issued:</strong> ".$row['ISSDTTM']."<br>";
}
sqlsrv_free_stmt( $sql_permit_info);
// Plan Review History Section
echo "<h3>Plan Review History</h3>";
echo "<div class='nimbus_table_minimal'>";
echo "<table cellspacing='0' cellpadding='0'>
<thead>
<tr>
<th>Description</th>
<th>Added</th>
<th>Status</th>
<th>Status Date</th>
<th>Dept</th>
</tr>";
$sql_review_history = sqlsrv_query($conn, $query_review_history);
while($row = sqlsrv_fetch_array($sql_review_history, SQLSRV_FETCH_ASSOC)){
echo "<tbody>";
echo "<tr>";
echo "<td>".$row['DESCRIPT']." ".$row['TYPENO']."</td>";
echo "<td>".$row['ADDDTTM']."</td>";
echo "<td>".$row['STAT']."</td>";
echo "<td>".$row['STATDTTM']."</td>";
echo "<td>".$row['DEPT']."</td>";
echo "</tr>";
}
sqlsrv_free_stmt( $sql_review_history);
echo "</tbody></table></div>";
// Application Inspection History Section
echo "<h3>Application Inspection History</h3>";
echo "<div class='nimbus_table_minimal'>";
echo "<table cellspacing='0' cellpadding='0'>
<thead>
<tr>
<th>Number and Inspection</th>
<th>Status</th>
<th>Inspector</th>
</tr>";
$sql_insp_history = sqlsrv_query($conn, $query_insp_history);
while($row = sqlsrv_fetch_array($sql_insp_history, SQLSRV_FETCH_ASSOC)){
echo "<tbody>";
echo "<tr>";
echo "<td>".$row['DESCRIPT']." ".$row['TYPENO']."</td>";
echo "<td>".$row['STAT']."</td>";
echo "<td>".$row['EMPLAST']."</td>";
echo "</tr>";
}
sqlsrv_free_stmt( $sql_insp_history);
echo "</tbody></table></div>";
}
?>
</div>
Upvotes: 0
Views: 1094
Reputation: 11
Something must be incorrect with the View I was using. I pulled the raw SELECT statement out of the view and inserted into my $sql_permint_info
and all is working fine now.
Upvotes: 0
Reputation: 108641
According to its documentation, sqlsrv_query() returns false
if it gets an error or a statement resource on success.
You must get in the habit of checking errors in SQL operations, or you'll never know whose woods you stop near, Mr. Frost.
You want something like this:
$sql_permit_info = sqlsrv_query($conn, $query_permit_info);
if ($sql_permit_info === false) {
die( print_r( sqlsrv_errors(), true));
}
while ($row= sqlsrv_fetch_array($sql_permit_info, SQLSRV_FETCH_ASSOC)) {
Upvotes: 1