PHP queries MSSQL.

Source: Internet
Author: User
PHP queries MSSQL. In MSSQL, field NAME, field content is Chinese, field type VACHAR (50), sorting rule: SQL _Latin1_General_CP1_CI_AS (this cannot be changed)

The PHP connection SQL queries other related content to show that the NAME can display Chinese characters normally, but I cannot query the NAME. Do I need to convert the encoding?

E. g.: select * from username where id = 123
The name is Zhangsan.

But in turn: select * from username where name = John
I cannot find it.

I know I can change the sorting rules, but it is garbled elsewhere. Therefore, the sorting cannot be changed.


Reply to discussion (solution)

Select * from username where name = 'zhangsan'

The same is not a single quotation mark.

First, make sure that your SQL commands do not have syntax errors.
Then we can test the effective writing method.

You can change the character set of the condition one by one, and it may be successful once.

SQL _Latin1_General_CP1_CI_AS
It is the first set of European annotation character sets (single byte) and theoretically covers all characters
However, there is actually an internal character set conversion problem. whether multi-byte character sets can be normally supported is a problem.

I remember that when I was reading a book, using mysql to create a database was also a latin character set. when I was writing a project, I could also store it, but I couldn't find the search function, later I want to convert the data to utf8, but the database is full of Latin .. At that time, I just created a casual exercise project, so I didn't have to worry about it. I 'd better keep the code consistent with the project. Otherwise, it would be more troublesome in the future.

It is strange that, if I query other content in the table, the Chinese name in the name can be displayed normally, and the Chinese name cannot be searched directly.
The content stored in the NAME field is ö~~ä( Chinese) ~n ~& pide; (search) I can query this garbled SQL query.

You have pasted the name queried by other content in base64 encoding.
For example, if the name field of the output message is included in $ name
Echo base64_encode ($ name );
Post result

In the gbk environment

Header ('content-type: text/html; charset = utf-8 '); echo utf8_encode ('Chinese'); // öð Î äecho utf8_encode ('search '); // predictionalist & pide;

You have pasted the name queried by other content in base64 encoding.
For example, if the name field of the output message is included in $ name
Echo base64_encode ($ name );
Post result

In the gbk environment

Header ('content-type: text/html; charset = utf-8 '); echo utf8_encode ('Chinese'); // öð Î äecho utf8_encode ('search '); // predictionalist & pide;



Thank you for your reply.

$ Str = utf8_encode ('Chinese'); echo $ str; echo base64_encode ($ str); $ r = mssql_query ("select * from users where Name = '". $ str. "'");


The display converts Chinese characters and copies them to the SQL tool for query. why is it still not found in php?

Try your SQL statement in phpmyadmin first.

You are not select * from username where id = 123
The name is Zhangsan.
I asked you to post the base64 encoding of the name value

You are not select * from username where id = 123
The name is Zhangsan.
I asked you to post the base64 encoding of the name value



In this way, Chinese characters can be displayed normally.
Chinese BASE64 1 tDOxA = BASE64 y9HL9w =

How do you get a base64 encoded string?
It won't be echo base64_encode ('Chinese'); is that true?

No. I found the results,
Php query statement:

$r=mssql_query("select *  from users where denglu=1");

The query result is directly BASE64:
base64_encode($row[1])

How do you get a base64 encoded string?
It won't be echo base64_encode ('Chinese'); is that true?



No. I found the results,
Php query statement:
$r=mssql_query("select *  from users where denglu=1");


The query result is directly BASE64:
base64_encode($row[1])


If it is convenient for the moderator, I will show it to you via QQ.

Encoding problem, your php default encoding is inconsistent, the value passed to mysql is not 'Zhang San', of course, cannot be queried.
Try php header ("Content-type: text/html; charset = latin1 ");

Encoding problem, your php default encoding is inconsistent, the value passed to mysql is not 'Zhang San', of course, cannot be queried.
Try php header ("Content-type: text/html; charset = latin1 ");



I have changed php encoding to UTF8. Statement output is also:
Select * from server01.dbo. cabal_character_table where name = 'öð Î ä' // the display in the database is the same as that in this case. you can directly execute this command in the SQL tool to find the UTF-8 encoded character, original: Chinese

Where name = 'öð Ä ä'

Is your garbled value garbled in data?

Where name = 'öð Ä ä'

Is your garbled value garbled in data?



Yes, this is the case in the database.



I am wondering if mssql has any conversion statements to convert the data type of the column name during query. For example:

Select here, convert the NAME to hexadecimal or CHAR from name where name = here, convert the character to hexadecimal or CHAR

I'm curious about why php can be converted to Chinese by querying other column names such as name = Zhang San id = 1 and id = 1,

Php default settings GB2312, mssql is GBK, name this column is varchar (50) storage method is ISO-8859-1

First, you need to solve the problem of garbled data in the database. The next step is to read the data encoding.

First, you need to solve the problem of garbled data in the database. The next step is to read the data encoding.



Database garbled characters cannot be solved. Not under my control. You can only try to read the data.

When you store the data in the format of utf8 or gbk
Save as latin1 encoding
You can use utf8 or gbk to obtain the correct Chinese character.
If you use latin1 encoding, the garbled characters in the database should be identical.
If you want to query Chinese characters through database operations, you should query the encoding of gbk to latin1.
Boring... iconv seems unable to convert utf8 Chinese to ISO-8859-1 // IGNORE
I personally don't think it's really interesting. you can't use fuzzy search in this way.
The data is saved in this way, and the operation direction is very small.

You can run select * from username where name = 'zhangsan' directly in the database.

Change the connection Library encoding, UTF8

You have fixed it yourself. Closed the post. Thank you for your enthusiastic answers and help! Thank you !!

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.