PHP MSSQL查询搜索

时间:2013-10-15 17:53:44

标签: php sql sql-server

我已经打了一段时间,并且不能让变量适用于搜索。可以连接正常并返回定义为正确的mssql_query的结果,也可以通过变量等进行精确排序,但尝试实现文本搜索并不会返回结果,不确定原因。

<?php

$link = mssql_connect('SERV13\\RALSQL12', 'RA4joomla', 'Fenestron1');

if (!$link || !mssql_select_db('RALNHV', $link)) {
    die('Unable to connect or select database!');
}else{
echo"";
}

if(isset($_REQUEST['submit'])){
    $firstname=$_POST['FirstName'];
    $surname=$_POST['Surname'];
    $query = 'SELECT * FROM lEmployee WHERE FirstName LIKE '%".$firstname."%' OR Surname LIKE '%".$surname."%'';
    $q=mssql_query($sql);
}
else{ 
    $query = 'SELECT * FROM lEmployee';
    $q = mssql_query($query);
}
?>
<form method="post">
    <table width="200" border="1">
  <tr>
    <td>Name</td>
    <td><input type="text" name="firstname" value="<?php echo $firstname;?>" /></td>
    <td>Email</td>
    <td><input type="text" name="surname" value="<?php echo $surname;?>" /></td>
    <td><input type="submit" name="submit" value=" Find " /></td>
  </tr>
</table>
</form>
<?php
// Check if there were any records
echo "<table class='table'>"; 
echo "<tr>";  
echo "<th><a href='?orderBy=FirstName'>FirstName</a></th><th><a href='?orderBy=Surname'>Surname</a></th><th><a href='?orderBy=EmployeeNo'>Trigram</a></th><th>Office Phone</th><th>Mobile</th><th><a href='?orderBy=EmployeeJobTitle'>Job Title</a></th><th><a href='?orderBy=Name'>Base</a></th>";
echo "</tr>";

    while ($row = mssql_fetch_array($query)) {
        echo "<tr>";  
            echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' .  iconv("CP1252", "UTF-8", $row['FirstName']) . '</a>' . "</td>";  
            echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . iconv("CP1252", "UTF-8", $row['Surname']) . '</a>' . "</td>";
            echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . $row['EmployeeNo'] . '</a>' . "</td>";  
            echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . $row['Phone'] . '</a>' . "</td>";
            echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . $row['Mobile'] . '</a>' . "</td>";
            echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . $row['EmployeeJobTitle'] . '</a>' . "</td>";
            echo "<td>" . '<a href="user-profile-id?id=' . $row['ID'] . '" class="modal">' . $row['Name'] . '</a>' . "</td>";

        echo "</tr>"; 
    }
echo "</table>";
?>

1 个答案:

答案 0 :(得分:1)

我100%同意nickL您在查询中遇到一些格式化问题尝试用此替换您的搜索查询:

    $firstname=$_POST['firstname'];
    $surname=$_POST['surname'];
    $query = "SELECT * FROM lEmployee WHERE FirstName LIKE '%".$firstname."%' OR Surname LIKE '%".$surname."%'";
    $q=mssql_query($sql);
php是一个区分大小写的语言你的帖子变量名称是错误的替换代码然后重试,如果没有成功,请尝试echo $ query并在sql server的查询浏​​览器中运行它。

希望这能解决问题。