PHP query MSSQL appears garbled solution _php tips

Source: Internet
Author: User
Tags mssql php example

The example of this article tells the PHP query MSSQL garbled resolution method. Share to everyone for your reference. The specific analysis is as follows:

In the PHP connection MSSQL query out of all is garbled, this problem I know according to experience is coding problem, below to give you a summary of the solution.

method One, modify php.ini file , of course, according to your page situation to set or can be UTF-8 encoded, the code is as follows:

Copy Code code as follows:
; mssql.charset = "Iso-8859-1"

Mssql.charset = "GBK"

method Two, direct conversion in the program , the code is as follows:

Copy Code code as follows:
Iconv (' GB2312 ', ' UTF-8 ', $data)

method Three, use ADO connection to set up the code on the connection, as follows:

Copy Code code as follows:
$conn = new COM ("ADODB. Connection ", NULL, Cp_utf8) or Die (" Cannot start ADO ");

PHP Example , the code is as follows:

Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8″>
<body>
<?php
Print ("The next line generates an error.www.jb51.net<br>");
Printaline ("please?");
Print ("This won't be displayed due to the above error.");
?>
<?php
$conn = new COM ("ADODB. Connection ", NULL, Cp_utf8) or Die (" Cannot start ADO ");
How Access databases are opened
$conn->open ("Provider=Microsoft.Jet.OLEDB.4.0; Data source= $db ");
$conn->open ("Driver={microsoft Access DRIVER (*.mdb)};" dbq= $db ");
$conn->open ("Driver={sql Server}"; server={192.168.22.40};D atabase=sugarcrm_db; Uid=sa; pwd=123456; ");
Execute Query and output data
$rs = $conn->execute (' SELECT * from accounts ') or Die ("error Query");
?>
<table border= "1″>
<tr><th>ID</th><th>Title</th>
</tr>
<?php
while (! $rs->eof) {
Echo ' <tr> ';
Echo ' <td> '. $rs->fields[' id ']->value. ' </td> ';
Echo ' <td> '. $rs->fields[' name ']->value. ' </td> ';
Echo ' </tr> ';
$rs->movenext ();
}
?>
</table>
<?php
Releasing resources
$rs->close ();
$conn->close ();
$rs = null;
$conn = null;
?>
</body>

Summary:

One is: the type of database, including, database, table, field three should be unified, you can check

The second is: File encoding type, if you use DW or EditPlus can view page encoding, different need to modify

Three is: Access to the database when the setting is set NAMES UTF8;

Four is: Browser display mode, add meta attribute <meta charset=utf-8>

I hope this article will help you with your PHP program design.

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.