PHP,sqlsrv连接无法正常工作

时间:2016-01-11 17:18:43

标签: php sql-server search

您正在尝试通过php连接到sql server,搜索并显示基于搜索字符串的结果。以下是index.php

成功通过编写查询而不是通过此搜索表单

单独获取结果

以下是search.php

  <?php

$button = $_GET ['submit'];
$search = $_GET ['search']; 

if(!$button)
echo "you didn't submit a keyword";
else
{
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";

}
$serverName = "xxxx";
$connectionInfo = array( "Database"=>"xxxx", "UID"=>"xxxx", "PWD"=>"xxxx");
$conn = sqlsrv_connect( $serverName, $connectionInfo );

$Query = "SELECT employee_name FROM employees WHERE employee_name like '%".$search."%'";
$run = sqlsrv_query($conn,$Query);


$foundnum = sqlsrv_num_rows($run);

if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.";
else
{
echo "$foundnum results found !<p>";

while($row = SQLSRV_FETCH_ARRAY($run,SQLSRV_FETCH_ASSOC))
{
    echo $row['Employee_name'];
    }
sqlsrv_free_stmt($run);
}
}
?>

1 个答案:

答案 0 :(得分:0)

使用

这适用于mysqli连接

$con = mysqli_connect("localhost","my_user","my_password","my_db");

这适用于mysql连接

$con = mysql_connect("localhost","my_user","my_password","my_db");