**已编辑以获取更多信息
我在创建搜索页面以显示SQL Server数据库的结果时遇到问题。我的SQL是正确的,因为它输出了我想要的SSMS。我的连接信息也是正确的,因为我已经测试过了。我的问题来自于将其绑定到搜索表单中 - 我无法找到任何结果。如果我想要它打印一个表,它打印就好了,但是,我真的需要它作为搜索工作。我对PHP和SQL一般都很陌生,但这是我到目前为止所做的:
$serverName = "myserver";
$connectionInfo = array( "Database"=>"mydb", "UID"=>"myuser", "PWD"=>"mypass");
$conn = sqlsrv_connect( $serverName, $connectionInfo) OR die ('broke:' .sqlsrv_errors());
$output = '';
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i", "", $searchq);
$query = sqlsrv_query($conn, "SELECT columns_I_need
FROM tables_I_need
WHERE col_1 LIKE '%$searchq%'") or die('broke:' .sqlsrv_errors());
$count = sqlsrv_num_rows($query);
if($count == 0) {
$output = 'There was nothing';
} else {
while($row = sqlsrv_fetch_array($query)) {
$stname = $row['streetname'];
$stno = $row['streetnumber'];
$apno = $row['permitnumber'];
$output .= '<div> '.$stname.' '.$stno.'</div>';
}
}
}
我的搜索表单如下:
<form> action="" method="post">
<input type="text" name="search" placeholder="Search for address...">
<input type="submit" value="Submit">
</form>
<?php print("$output"); ?>
每次我搜索都没有结果。所以我想我的问题是:我的搜索表单是否正确并且我的WHERE col_1 LIKE '%$searchq%'
是否正确?我错过了什么?另外,preg_replace
无助于防止sql注入吗?
如果这个描述不够,请告诉我。
答案 0 :(得分:0)
我认为问题与这条线有关:
$count = sqlsrv_num_rows($query);
来自php.net sqlsrv_num_rows()
的文档(强调我的)。
<强>参数强>
返回行计数的语句。 必须使用静态或键集游标创建参数资源。参数 - sqlsrv_num_rows
和
返回值
返回成功时检索的行数,如果发生错误则返回FALSE。 如果使用前向光标(默认)或动态光标,则返回FALSE。返回值 - sqlsrv_num_rows
由于您未能在查询中指定游标,因此无论查询是否返回结果,都会使用默认游标并返回false
。
然后在if
语句中,检查$count == 0
。此检查将返回true
,因为0
可以评估为false
,并且您没有使用严格类型比较(===
)。因此,你总会看到&#34;没有什么&#34;。
要解决此问题,您需要为查询指定 static 或 keyset 游标。我将以SQLSRV_CURSOR_STATIC
作为我的光标作为示例,但您可以使用SQLSRV_CURSOR_KEYSET
。 See here了解有关不同游标的更多信息。
一般语法是:
$stmt = sqlsrv_query($conn, $query, $params, array("Scrollable" => SQLSRV_CURSOR_STATIC));
答案 1 :(得分:0)
我想我已经确定了这一点。它显示我需要的所有内容,我相信它被设置为参数化查询,并且使用我的类标签我可以设置输出样式。
我唯一的问题是,如果有人输入街道号码并且街道名称或街道名称是两个或更多单词,我不会得到任何结果。我的$sql
语句可能很简单。我试过了:
WHERE (STNO LIKE '%$search_term%' AND STNAME LIKE '%$search_term%') OR APNO LIKE '%$search_term%'
但这也没有让我得到任何东西。
$serverName = "myserver";
$connectionInfo = array( "Database"=>"mydb", "UID"=>"myuser",
"PWD"=>"mypass");
$conn = sqlsrv_connect( $serverName, $connectionInfo) OR die ('broke:'
.sqlsrv_errors());
$search_term = $_GET['query'];
$sql = "SELECT my rows FROM my tables
WHERE STNO LIKE '%$search_term%' OR STNAME LIKE '%$search_term%' OR APNO LIKE '%$search_term%'
ORDER BY STNO";
echo "<div class='search-term-display'>You searched for: ", $search_term, "</div>";
if(isset($_GET['query'])) {
$search_term = $_POST['query'];
$search_term = preg_replace("#[^0-9a-z]#i", "", $search_term);
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => 'static'));
while($data = sqlsrv_fetch($query)) {
while($row = sqlsrv_fetch_array($query)){
$stno = $row['STNO'];
$stname = $row['STNAME'];
$apno = $row['APNO'];
$suffix = $row['SUFFIX'];
$hseptyp = $row['HSEPTYP'];
$dttm = $row['COMPDTTM']->format('d/m/Y');
$partial = $row['PARTIAL'];
$waived = $row['WAVIED'];
$failed = $row['FAILED'];
$inspcomments = $row['INSPECTIONCOMMENTS'];
$fcomments = $row['FAILEDCOMMENTS'];
$descript = $row['DESCRIPT'];
echo "<hr>";
echo "<div class='insp_address'>".$stno.' '.$stname.' Permit Number: '.$apno."</div>";
echo "<div class='insp_date'>".'Inspection Date: '.$dttm."</div>";
echo "<div class='sys_type'>".'Septic System Type: ' .$hseptyp."</div>";
echo "<div class='code_violation_status'>".'Code Violation Status: ' .$descript."</div>";
echo "<div class='code_violation'>".'Code: '.$failed."</div>";
echo "<div class='insp_comments_title'>Inspection Comments:</div>";
echo "<div class='insp_comments'>".$fcomments."</div>";
}
sqlsrv_free_stmt( $query);
}
}
echo "</div>";
答案 2 :(得分:0)
这是我使用查询和键集获取的方式:
$this->result = SQLSRV_QUERY($this->conn, $this->sql, Array(), Array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
SQLSRV_FETCH($this->result,SQLSRV_SCROLL_RELATIVE,$this->offset);
for($row = 1; $row <= $this->items_per_page; $row++) {
$rows[] = SQLSRV_FETCH_ARRAY($this->result);
}
return $rows;
然后您可以像这样使用foreach:
<?php foreach ($result as $rows) {
if ($rows != NULL) { ?>
<tr>
<td><?php echo $rows['TerritoryID']?></td>
<td><?php echo $rows['TerritoryDescription']?></td>
<td><?php echo $rows['RegionID']?></td>
</tr>
<?php } ?>
<?php } ?>
忘记添加一个好的行数块:
Function get_total_rows() {
$row_count = SQLSRV_QUERY($this->conn, $this->sql, Array(), Array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
return SQLSRV_NUM_ROWS($row_count);
SQLSRV_FREE_STMT($row_count);
}