dongqun9403 2016-03-01 05:12
浏览 32
已采纳

带SQL Server的PHP(请不要使用MySQL) - 在WHERE子句中选择带有LIKE的语句

I hate PHP, but I have to do this. I have spent the last 2 days searching for a simple way to write a SQL Select with a LIKE clause where the parameter is passed from the lname input text on the form. Now, it has to be SQL Server, NOT MYSQL

So here is what I've done so far.

    function getActorDetailsLnameOnly($lname) {
    // the SQL query to be executed on the database

    $query = "select NameFirst, NameLast, Age, Gender from actor where NameLast like '%$lname%'";

    return executeQuery($query);
}

on the index.php, I wrote the following:

if ((!empty($_REQUEST['lname'])) and ( empty($_REQUEST['age']) and ( empty($_REQUEST['gender'])))) {

        $lname = (string) $_GET['lname'];

        $sql = getActorDetailsLnameOnly($lname);
        foreach ($sql as ...) {
            extract(...);
           ...

The code returns a value, but it's nowhere near correct. It's like requesting A in the select statement and it's returning Z. I can't figure it out.

  • 写回答

1条回答 默认 最新

  • duanliang789262 2016-03-01 10:23
    关注

    You check form input values through $_REQUEST['lname'] and then assign a variable $lname = (string) $_GET['lname'];. If form method is POST then $_REQUEST['lname'] would have the value and $_GET['lname'] would be empty. As the result like pattern would be '%%', which is effectively everything but NULL.

    Basically, $_GET is for GET, $_POST is for POST and $_REQUEST is for any.

    Try using $lname = (string) $_REQUEST['lname'];.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?