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'];.

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

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题