When ThinkPHP2.0 reads MSSQL, the system prompts incorrectsyntaxnearthekeyword'

Source: Internet
Author: User
This article mainly introduces ThinkPHP2.0 reading MSSQL prompt Incorrectsyntaxnearthekeyword ThinkPHP

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);   } } ?>

The problem is that the query can be used to correctly read the data, but the M method cannot be used to read the data. an Incorrect syntax near the keyword 'as' error is returned.
The reason is that there is a problem with the query statement driven by DbMssql. class. php.

Because the MSSQL driver of TP2.0 is effective for SQL 2005, but is invalid for Version 2000, the reason is that the ROW_NUMBER function is not available in version 2000, 2005 has this function, as if to provide convenience and efficiency for data paging.

I hope the official team can add a 2000 Driver to TP2.0. Currently, the temporary solution is to modify ThinkPHP \ Lib \ Think \ Db \ Driver \ DbMssql. class. php: add '//' to the front of the line 25 protected $ selectSql '//'
And 326th rows

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; } 

Changed:

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

After this change, it can basically meet the general SQL requirements, but cannot use LIMIT, because the LIMIT method of MSSQL 2000 is top N
Implemented in this way;

If you are in trouble, use the Adodb class library, which provides a lot of support for MSSQL. The method to combine the Adodb class library is as follows:

First download the Adodb class library and decompress it to the ThinkPHP's vender directory, and rename adodb. inc. php to adodb. php.
Then, create a CommonAction. class. php file in the project Lib

<?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;   } } ?>

This CommonAction. class. php file 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 the Thinkphp module to perform simple data queries and also use Adodb to query paging data. there is really no way to do this. this is a stupid way, we still hope ThinkPHP can provide a perfect driver for MSSQL 2000.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.