0

我试图为组合框完成自动完成功能,我按照http://dev.sencha.com/deploy/ext-4.1.0-gpl/examples/form/forum-search.html做到了

我的 php 查询:

if (!$con) {
    throw new Exception("Error in connection to DB");
     }

$sql = "SELECT first_name FROM students WHERE first_name ILIKE '%$query%' ORDER BY name ASC";

$result = pg_query($con, $sql);
ORDER BY name ASC";


 while ($row = pg_fetch_array($result))
       {  
        $names[] = array('name' => $row[0]);

       }

$name_list = array();
$name_list['success']       = true;
$name_list['names']     = $names;


$name_list['totalCount']    = count( $name_list['names'] );

echo json_encode($name_list);

在我的情况下,用户可以输入姓氏而不是学生的名字,因此我需要编辑查询以使其在检查名字的结果为空后检查姓氏。这是我所做但没有工作的:

$sql = "SELECT first_name FROM students WHERE first_name ILIKE '%$query%' ORDER BY name ASC";
$result = pg_query($con, $sql);
if(pg_num_rows($result) == 0)
$sql = "SELECT last_name FROM students WHERE last_name ILIKE '%$query%' ORDER BY name ASC";
4

2 回答 2

0

请尝试以下修改:它只是 PHP 手册中的一个示例,请相应地更改查询

<?php
  $result=pg_query($conn, "SELECT * FROM x WHERE a=b;");
  if  (!$result) {
    echo "query did not execute";
  }
  $rs = pg_fetch_assoc($result);
  if (!$rs) {
    echo "0 records"
  }
?>
于 2012-12-01T05:39:55.187 回答
0

使用 OR 条件:

SELECT first_name, last_name
FROM students 
WHERE first_name ILIKE '%$query%'
   OR last_name ILIKE '%$query%';

您还应该阅读术语“SQL 注入”。您的代码易受攻击。

于 2012-12-01T08:53:06.990 回答