PHP read/write MSSQL garbled!

Source: Internet
Author: User
Tags dsn mssql

Recently doing PHP crawl, php+mssql collocation. Oh. Oh.. Looking for information on the Internet is very few. It seems to be a big upset. Look east. Look at the West. Finally, let me get something to tinker with. (recently there will be a php+mssql of information come out oh:>). Go to the chase.

PHP Crawl is crawling out of it. Crawl news headlines and news content. The first thing to do is to display the captured title and content directly.

The Default_charset key setting in PHP.ini is this: Default_charset = "Utf-8"

But the light capture takes off to deposit the database.

This is a problem.

Here is the join database function

PHP code*******************************************************

function insert_db ($title, $content, $lie)
{
   $dsn = ' Driver={sql Server}; Server=127.0.0.1;database=test ';
$link =odbc_connect ($dsn, ' sa ', ' admin ') or die (' Eeeor ');
Odbc_exec ($link, "INSERT INTO News (". $lie. ", content) VALUES ('". $titile. "', '". $content. "')");
Odbc_close ($link);
}

*************************************************************************************************************** **

But to the database a look miserable. It's all garbled. I looked for an article online. Look at the original because of the text encoding problem.

The default is CHINESE_PRC rule encoding ordering in MSSQL. And I set in PHP is UTF-8 so it will be garbled. But I set the php.ini default_charset item to GB2312 crawl to the title and content into MSSQL is also garbled. Depressed. Let's just ignore that.

Since the coding is wrong. Then the unification is not OK ...

Use the following function: Iconv (), encode the specified content and return the encoded content ...

Iconv ("Utf-8", "gb2312", $title); This translates the captured title into MSSQL. Look at the ha. The conversion succeeded. A slip of the Chinese title. Without any garbled characters. Oh. Oh...

Because PHP is UTF-8 so the time to correspond oh ...

Iconv ("gb2312", "Utf-8", $title), so that you can display the normal content on the Web page!!!

It's done!

PHP read/write MSSQL garbled!

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.