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. Online

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:

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.

  1. ; Valid range 0-2147483647. Default = 4096.
  2. ; Mssql. textlimit = 4096
  3. ; Valid range 0-2147483647. Default = 4096.
  4. ; 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 I switched to Convert (text, content) again, without any effect, and 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 changed odbc_connect to mssql_connect.

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:

  1. // Connect to MSSQL
  2. $ Link = mssql_connect ('kallespcsqlexpress ', 'sa', 'phpfi ');
  3. If (! $ Link! Mssql_select_db ('php', $ link ))
  4. {
  5. Die ('unable to connect or select database! ');
  6. }
  7. // Do a simple query, select the version
  8. // MSSQL and print it.
  9. $ Version = mssql_query ('select @ version ');
  10. $ Row = mssql_fetch_array ($ version );
  11. Echo $ row [0];
  12. // Clean up
  13. 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, and find:; mssql. textlimit = 4096 to mssql. textlimit = 2147483647

Locate:; mssql. textsize = 4096 to 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: 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, the long article is truncated, and changing ntext to nvarchar (max) in sql2008 will not 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.

  1. Include ("adodb/adodb. inc. php"); // contains the adodb class library file
  2. $ Conn = NewADOConnection ('odbc _ mssql'); // connect to the SQL Server database
  3. $ 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. MSSQL 2000 is supported only when dll (2000.80.2039.0) is copied to System32 or 5.2.11 in the PHP Directory. to support MSSQL 2005 and 2008, you need to install the driver provided by Microsoft.

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:

  1. // Connect to MSSQL
  2.  
  3. $ Link = mssql_connect ('kallespcsqlexpress ', 'sa', 'phpfi ');
  4. If (! $ Link! Mssql_select_db ('php', $ link ))
  5. {
  6. Die ('unable to connect or select database! ');
  7. }
  8. // Do a simple query, select the version
  9. // MSSQL and print it.
  10. $ Version = mssql_query ('select @ version ');
  11. $ Row = mssql_fetch_array ($ version );
  12. Echo $ row [0];
  13. // Clean up
  14. Mssql_free_result ($ version );

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.