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

带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.

我讨厌PHP,但我必须这样做。 在过去的两天里,我一直在寻找一种简单的方法来编写带有LIKE子句的SQL Select,其中参数是从表单上的 lname code>输入文本传递的。 现在,它必须是 SQL Server code>,而不是 MYSQL code> p>

所以这就是我到目前为止所做的。 p>

  function getActorDetailsLnameOnly($ lname){
 //要在数据库上执行的SQL查询
 
 $ query =“选择NameFirst,NameLast,年龄,性别 来自actor的名称'%$ lname%'“; 
 
返回执行查询($ query); 
} 
  code>  pre> 
 
 

索引 .php strong>,我写了以下内容: p>

  if((!empty($ _ REQUEST ['lname']))和(空($ _ REQUEST ['age]  '])和(empty($ _ REQUEST ['gender'])))){
 
 $ lname =(string)$ _GET ['lname']; 
 
 $ sql = getActorDetailsLnameOnly($ lname);  
 \ foreach($ sql as ...){
 extract(...); 
 ... 
  code>  pre> 
 
 

代码返回一个值,但是 它远远不够正确。 这就像在select语句中请求A并且它返回Z.我无法理解它。 p> div>

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