Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在sql server中使用TOP_Php_Sql Server_Php 7 - Fatal编程技术网

Php 如何在sql server中使用TOP

Php 如何在sql server中使用TOP,php,sql-server,php-7,Php,Sql Server,Php 7,我有以下代码: public function findAllByPagePlain($limit, $offset, $q, $category_id, $column, $order) { $query = "SELECT DISTINCT TOP $limit p.* FROM ec_product p "; $keywordQuery = "(p.name REGEXP '$q' OR p.status REGEXP '$q' OR p.descr

我有以下代码:

public function findAllByPagePlain($limit, $offset, $q, $category_id, $column, $order)
    {
        $query = "SELECT DISTINCT TOP $limit p.* FROM ec_product p ";
        $keywordQuery = "(p.name REGEXP '$q' OR p.status REGEXP '$q' OR p.description REGEXP '$q') ";
        if ($category_id != -1) {
            $query = $query . ", product_category pc WHERE pc.product_id=p.id AND pc.category_id=$category_id ";
            if ($q != "") $query = $query . "AND " . $keywordQuery;
        } else {
            if ($q != "") $query = $query . "WHERE " . $keywordQuery;
        }
        $query = $query . "ORDER BY p." . $column . " " . $order . " OFFSET $offset ";
        return $this->db->get_list($query);
    }
我正在尝试使用TOP从数据库中获取前n行。但它总是给我一个错误的说法

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '0'. [message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '0'. ) )

有人知道如何解决这个问题吗?

考虑以下几点:

“0”错误附近语法不正确的原因是,您没有为子句使用正确的语法。我认为,您正在尝试实现一个分页解决方案,所以请使用偏移量。。。FETCH子句而不是TOP子句。 REGEXP语法是MySQL的一部分,对于MS SQL Server,您应该使用LIKE或PATINDEX 作为一个重要的注意事项,请重写代码并使用参数化语句来防止注释中提到的SQL注入攻击。 更改以下示例中的函数,以解决“0”错误附近的错误语法:


选择顶部。。。从表中…按…订购。。。。如果需要使用DISTINCT,那么首先使用子查询,然后在子查询中使用TOP,例如:从SELECT DISTINCT*表T中选择TOP n列按SomeOrder排序这非常可怕。它对sql注入非常开放。调试是程序员工作的一部分。您是否尝试输出$query以查看查询的外观?
<?php
public function findAllByPagePlain($limit, $offset, $q, $category_id, $column, $order)
    {
        $query = "SELECT DISTINCT p.* FROM ec_product p ";
        $keywordQuery = "(p.name LIKE '%$q%' OR p.status LIKE '%$q%' OR p.description LIKE '%$q%') ";
        if ($category_id != -1) {
            $query = $query . ", product_category pc WHERE pc.product_id=p.id AND pc.category_id=$category_id ";
            if ($q != "") $query = $query . "AND " . $keywordQuery;
        } else {
            if ($q != "") $query = $query . "WHERE " . $keywordQuery;
        }
        $query = $query . "ORDER BY p." . $column . " " . $order . " OFFSET ". $offset . " ROWS FETCH NEXT " . $limit. " ROWS ONLY";
        return $this->db->get_list($query);
    }
?>