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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀