THINKPHP2.0 read the MSSQL hint incorrect syntax near the keyword ' as ' solution _php instance

Source: Internet
Author: User
Tags mssql

The problem code is as follows:

<?php 
class Indexaction extends action{public 
  function Index () {/ 
    * 
    $Model = new Model (); 
    $test = $Model->query (' select top * from F_city '); 
    Dump ($test); 
    * * 
    $CityModel = M (' city '); 
    $CityModel->find (); 
    Dump ($CityModel); 
>

The situation occurs when you use query to read the data correctly, but you cannot read it by using the M method, which reports the incorrect syntax near the keyword ' as '. Error
The reason is that there is a problem with the DbMssql.class.php-driven query statement.

Because TP2.0 's MSSQL drive is valid for SQL 2005, it is not valid for version 2000 because 2000 does not have Row_number function, 2005 has this function as if it is to facilitate and effect data paging.

I hope the authorities can give TP2.0 a 2000 drive, the current temporary treatment is to modify thinkphp\lib\think\db\driver\dbmssql.class.php, will line 25 of the protected $SELECTSQL Add '//' in front
and the No. 326 line of

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

To

Public Function Parselimit ($limit) {return 
  '; 
}

After this change, you can basically meet your normal SQL requirements, but you can't use limit, because the limit method for MSSQL 2000 is the top N
To achieve this;

If you feel trouble, then combine the Adodb class library, this to the MSSQL support relatively good many. To combine the Adodb class library My methods are as follows:

First download the Adodb class library and extract it into the thinkphp vendor directory and rename adodb.inc.php to adodb.php
Then create a CommonAction.class.php content in the project Lib for

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

You must refer to this CommonAction.class.php in other files in your 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 ']; 
     } 
  } 
? >

This can use thinkphp module for simple data query can also use ADODB for paging data query, it is no way, this is a stupid way oh, or hope thinkphp can out a MSSQL version 2000 can use the perfect drive.

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.