ThinkPHP 2.0 Read MSSQL Prompt Incorrect syntax near the keyword 'AS' Solution

  • 2021-07-06 10:14:34
  • OfStack

The problem code is as follows:


<?php 
class IndexAction extends Action{ 
  public function index(){ 
    /* 
    $Model = new Model(); 
    $test = $Model->query('select top 10 * from f_city'); 
    dump($test); 
    */ 
    $CityModel = M('city'); 
    $CityModel->find(); 
    dump($CityModel); 
  } 
} 
?>

What happens is that the data can be read correctly using query, but it cannot be read using M, and Incorrect syntax near the keyword 'AS' is reported. Error
The reason is that there is a problem with the query statement driven by DbMssql. class. php.

Since the MSSQL driver of TP 2.0 is valid for SQL 2005, but not for the 2000 version, because the ROW_NUMBER function is not available in the 2000 version, this function in 2005 seems to provide convenience and performance for data paging.

I hope the official can add a 2000 driver to TP2.0. At present, the temporary processing method is to modify ThinkPHP\ Lib\ Think\ Db\ Driver\ DbMssql.class.php, and add '//' before protected $selectSql in line 25
And on line 326


public function parseLimit($limit) { 
      if(emptyempty($limit)) $limit=1; 
  $limit    =    explode(',',$limit); 
  if(count($limit)>1) 
    $limitStr    =    '(T1.ROW_NUMBER BETWEEN '.$limit[0].' + 1 AND '.$limit[0].' + '.$limit[1].')'; 
      else 
    $limitStr = '(T1.ROW_NUMBER BETWEEN 1 AND '.$limit[0].")"; 
  return $limitStr; 
} 

Replace with the following:


public function parseLimit($limit) { 
  return ''; 
}

After this change, SQL requirements like 1 can be basically met, but LIMIT cannot be used, because the LIMIT method of MSSQL 2000 is based on top N
Achieved in this way;

If you find it troublesome, combine it with the Adodb class library, which supports MSSQL relatively well. My approach to combining the Adodb class library is as follows:

First download the Adodb class library and unzip it into the Vendor directory of ThinkPHP, and rename adodb. inc. php to adodb. php
Then create an CommonAction. class. php in the Lib of the project with the content of


<?php 
class CommonAction extends Action { 
  public $dbsql; 
  function _initialize() { 
    Vendor('adodb5.adodb'); 
    $adodb = ADONewConnection(C('DB_TYPE')); 
    $adodb->Connect(C('DB_HOST'), C('DB_USER'), C('DB_PWD'), C('DB_NAME')); 
    $adodb->SetFetchMode(ADODB_FETCH_ASSOC); 
    $this->dbsql = $adodb; 
  } 
} 
?>

The text CommonAction. class. php must be referenced in other files of the project to use ADODB, for example:


<?php 
class IndexAction extends CommonAction { 
  public function index() { 
    $query = $this->dbsql->Execute('select * from xxx'); 
    while($rows = $query->FetchRow()) { 
        echo $rows['fields']; 
     } 
  } 
} 
?>

In this way, we can use Thinkphp module for simple data query and Adodb for paging data query. It is really impossible to find a way. This is a stupid way. I still hope ThinkPHP can produce a perfect driver for MSSQL 2000 version.


Related articles: