Summary of ntext field errors in php and mssql

Source: Internet
Author: User

Php + mssql makes it really painful. The type of ntext fields used to read mssql in php has another problem: when the content of ntext is too long, the ntext fields are not completely read.

I have been searching for it on the internet for a long time. It is said that ntext reading in php is incorrect, but I was depressed if I didn't find my page error. Later I found out that php can only read 4 kb of content by default. It turns out that my content exceeds 4 kb. The modification method is as follows:

Below:

1. Upgrade php to php5.

Fortunately, I already have php5.

2. Adjust the parameters of php. ini to increase the default value of php to 4 K.

; Valid range 0-2147483647. Default = 4096.
; Mssql. textlimit = 4096

; Valid range 0-2147483647. Default = 4096.
; Mssql. textsize = 20480

 

";" Before mssql. textlimit and mssql. textsize, and then change the default value to a larger value, such as 20480 (20 K)

3. Restart the web server.


I thought the exciting time would come. When I run the php page, the effect was still the same as before, and the field data was not completely read. Later, I found out that php is not very friendly with ntext support and should be converted to text. So we use

Convert (text, content) is switched, without any effect, and I almost killed my iis. I have been searching for it online for a long time. When I was about to give up, I tried to change the database connection method. I put

The change of odbc_connect to mssql_connect succeeded.

 

Php link mssql problem ntext cannot be read


Data that can be linked to a database but cannot be read. Google and Baidu finally learned that the reverse return value of the ntext field of mssql read by php is null. We recommend that you change the ntext field to text.

If there is no ntext field in the table, use the following code:

The Code is as follows: Copy code

// Connect to MSSQL

$ Link = mssql_connect ('kallespcsqlexpress ', 'sa', 'phpfi ');

If (! $ Link! Mssql_select_db ('php', $ link ))

{

Die ('unable to connect or select database! ');

}

// Do a simple query, select the version

// MSSQL and print it.

$ Version = mssql_query ('select @ version ');

$ Row = mssql_fetch_array ($ version );

Echo $ row [0];

// Clean up

Mssql_free_result ($ version );

?

If the table contains an ntext military field and it is difficult to modify the text field, you can:

1. Modify php. ini

Open php. ini

Find:

; Mssql. textlimit = 4096

Change

Mssql. textlimit = 2147483647

Find:

; Mssql. textsize = 4096

Change

Mssql. textsize = 2147483647

2. you can modify fields. Because ntext and nvarchar fields in SQL server are stored in unicode encoding, php will encounter errors when reading fields with ntext and nvarchar types through mssql extension.

If the title field type is nvarchar and the content field type is ntext, the following SQL statement reports an error:

Incorrect:

Select title, content from article

Correct:

The Code is as follows: Copy code
Select convert (varchar (255), title) as title, convert (text, content) as content from article

{This method is not practical. If the ntext content is too long to be converted, the data will be lost. That is to say, the long article has been truncated, and changing ntext to nvarchar (max) in sql2008 won't work}

3. If you are a VM, you can use the adodb component to read the data. If your host does not support it, I cannot do it now.

The Code is as follows: Copy code

Include ("adodb/adodb. inc. php"); // contains the adodb class library file

$ Conn = NewADOConnection ('odbc _ mssql'); // connect to the SQL Server database

$ Conn-"Connect (" Driver = {SQL Server}; Server = localhost; Database = mydb; ", 'username', 'Password ');

{Note that if you use adodb, the driver problem is the premise that you use adodbPHP to connect to MSSQL. Before 5.2.10, the driver provided by PHP only supports MSSQL 7.0. If you want to support MSSQL 2000, you need to set ntwdblib of MSSQL 2000. dll (2000.80.2039.0) to the System32 or PHP Directory
5.2.11: MSSQL 2000 is supported. To support MSSQL 2005 and 2008, install the driver provided by Microsoft.

Therefore, there is basically no way to use a VM.

}

?>

 

The ntext field read from mssql is unoccupied.


During the test, you can clearly link to the database but cannot read the data. After searching for information, it is found that the reverse return value of the ntext field in mssql read by php is null. We recommend that you change the ntext field to text.

If there is no ntext field in the table, use the following code:

The Code is as follows: Copy code

// Connect to MSSQL
$ Link = mssql_connect ('kallespcsqlexpress ', 'sa', 'phpfi ');

If (! $ Link! Mssql_select_db ('php', $ link ))
{
Die ('unable to connect or select database! ');
}

// Do a simple query, select the version
// MSSQL and print it.
$ Version = mssql_query ('select @ version ');
$ Row = mssql_fetch_array ($ version );

Echo $ row [0];

// Clean up
Mssql_free_result ($ version );
?>
 


If the table contains an ntext military field and it is difficult to modify the text field, you can:

1. Modify php. ini

Open php. ini

Find:

; Mssql. textlimit = 4096


Change

Mssql. textlimit = 2147483647


Find:

; Mssql. textsize = 4096


Change

Mssql. textsize = 2147483647


2. you can modify fields. Because ntext and nvarchar fields in SQL server are stored in unicode encoding, php will encounter errors when reading fields with ntext and nvarchar types through mssql extension.

If the title field type is nvarchar and the content field type is ntext, the following SQL statement reports an error:

Incorrect:

Select title, content from article


Correct:

The Code is as follows: Copy code
Select convert (varchar (255), title) as title, convert (text, content) as content from article

3. If you are a VM, you can use the adodb component to read the data. If your host does not support it, I cannot do it now.

The Code is as follows: Copy code
Include ("adodb/adodb. inc. php"); // contains the adodb class library file
$ Conn = NewADOConnection ('odbc _ mssql'); // connect to the SQL Server database
$ Conn-> Connect ("Driver = {SQL Server}; Server = localhost; Database = mydb;", 'username', 'Password ');
?>

 

Related Article

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.