正则表达式搜索连接到数据库的数据表

时间:2016-11-10 02:26:17

标签: php mysql regex datatables

我正在尝试能够在连接到数据库的datatables中进行正则表达式搜索。

我目前处理搜索的代码(见下文3):

if( !empty($requestData['search']['value']) ) {
    // if there is a search parameter
    $sql = "SELECT first_name, last_name, position, office, start_date, salary";
    $sql.=" FROM $table";
    $sql.=" WHERE first_name LIKE '%".$requestData['search']['value']."%' ";    // $requestData['search']['value'] contains search parameter
    $sql.=" OR last_name LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR position LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR office LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR start_date LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR salary LIKE '%".$requestData['search']['value']."%' ";

    $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees-456");
    $totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result without limit in the query 

    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   "; // $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc , $requestData['start'] contains start row number ,$requestData['length'] contains limit length.
    $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees-789"); // again run query with limit

} else {    

    $sql = "SELECT first_name, last_name, position, office, start_date, salary";
    $sql.=" FROM $table";

    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
    $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees 000");

}

我希望能够做的是在数据中使用正则表达式(参见下面的2)。如何编写上述代码来执行此操作?

我试过这个(用REGEXP替换了LIKE并删除了%)但是无法让它工作,我开始怀疑它是否还有更多呢?

$sql = "SELECT first_name, last_name, position, office, start_date, salary";
$sql.=" FROM $table";
$sql.=" WHERE first_name REGEXP '".$requestData['search']['value']."' ";    // $requestData['search']['value'] contains search parameter
$sql.=" OR last_name REGEXP '".$requestData['search']['value']."' ";
$sql.=" OR position REGEXP '".$requestData['search']['value']."' ";
$sql.=" OR office REGEXP '".$requestData['search']['value']."' ";
$sql.=" OR start_date REGEXP '".$requestData['search']['value']."' ";
$sql.=" OR salary REGEXP '".$requestData['search']['value']."' ";

1 - 这是我的数据集:

mysql> SELECT * FROM datatables_demo;                                           +----+------------+-----------+-------------+--------+------------+--------+
| id | first_name | last_name | position    | office | start_date | salary |
+----+------------+-----------+-------------+--------+------------+--------+
|  1 | Tiger      | Nixon     | Accountant  | Tokyo  | 2016-11-08 | 320800 |
|  2 | Garrett    | Winters   | Accountant2 | Tokyo  | 2016-11-08 | 170750 |
|  3 | Ashton     | Cox       | Accountant3 | Tokyo  | 2016-11-08 |  86000 |
|  4 | Cedric     | Kelly     | Accountant4 | Tokyo  | 2016-11-08 | 433060 |
|  5 | Tiger5     | Nixon     | Accountant  | Tokyo  | 2016-11-08 | 320800 |
+----+------------+-----------+-------------+--------+------------+--------+
5 rows in set (0.00 sec)

2 - 这是一个正则表达式的例子:

mysql> SELECT first_name, last_name, position, office, start_date, salary FROM datatables_demo WHERE first_name REGEXP '^[TG]';
+------------+-----------+-------------+--------+------------+--------+
| first_name | last_name | position    | office | start_date | salary |
+------------+-----------+-------------+--------+------------+--------+
| Tiger      | Nixon     | Accountant  | Tokyo  | 2016-11-08 | 320800 |
| Garrett    | Winters   | Accountant2 | Tokyo  | 2016-11-08 | 170750 |
| Tiger5     | Nixon     | Accountant  | Tokyo  | 2016-11-08 | 320800 |
+------------+-----------+-------------+--------+------------+--------+
3 rows in set (0.00 sec)

3 - 这是一个常规搜索示例:

mysql> SELECT first_name, last_name, position, office, start_date, salary FROM datatables_demo WHERE first_name LIKE '%ti%';
+------------+-----------+------------+--------+------------+--------+
| first_name | last_name | position   | office | start_date | salary |
+------------+-----------+------------+--------+------------+--------+
| Tiger      | Nixon     | Accountant | Tokyo  | 2016-11-08 | 320800 |
| Tiger5     | Nixon     | Accountant | Tokyo  | 2016-11-08 | 320800 |
+------------+-----------+------------+--------+------------+--------+
2 rows in set (0.01 sec)

0 个答案:

没有答案