我正在尝试创建一个搜索栏来搜索我的数据库,但是它总是返回0个结果,
这是连接和搜索代码,
<?php
$serverName = "server";
$connectionInfo = array( "Database"=>"server");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$output ='';
if(isset($_POST['PartSelect'])){
$searchq = $_POST['PartSelect'];
$query = sqlsrv_query($conn, "SELECT * FROM Parts WHERE PartNumber LIKE '%$searchq%'") or die ("Could not search");
$count = sqlsrv_num_rows($query);
if($count == 0){
$output = 'There are no results';
-----本节是新的错误代码-----
$rows = sqlsrv_has_rows( $query );
if ($rows === true)
echo "\nthere are rows\n";
else
echo "\nno rows\n";
----结束部分------ 打印出来说它有行,所以它可以拾取数据,
}
else {
while ($row = sqlsrv_fetch_array($query)){
$Partno = $row['PartNumber'];
$output .= '<div>'.$PartNo. '</div>';
}
}
?>
这里是搜索栏和提交按钮(我正在使用物化作为框架),最后是输出。
<form action="Tests.php" method="post">
<div class="form-group">
<br/><input name="PartSelect" type="text" class="form-control" id="PartSelect" aria-describedby="nameHelp" placeholder="Enter Part">
<br/><button name="SelectPartbtn" id="Submitbtn" type="Submit" class="waves-effect waves-light btn">Find</button>
</div>
<?php print ("$output"); ?>
</form>
答案 0 :(得分:0)
尝试使用此代码检查连接是否建立
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = sqlsrv_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . sqlsrv_errors());
}