简体   繁体   中英

Using German characters in Microsoft Drivers for PHP for SQL Server

Hope all are doing fine. I have a MS Sql database that I want to use in php. My php version is 5.4 so I have installed Microsoft Drivers for PHP and Sql Server 'php_sqlsrv_54_ts.dll' in php. Beow is my code:

        $serverName = "Myserver";
    $connectionInfo = array( "Database"=>"database", "UID"=>"userid", "PWD"=>"password");
    $conn = sqlsrv_connect( $serverName, $connectionInfo );
    if( $conn === false ) {
        die( print_r( sqlsrv_errors(), true));
    }

    $sql = "select * from AVE_Stamm WHERE Strasse='Friesenstraße 20'";
    $stmt = sqlsrv_query( $conn, $sql );
    if( $stmt === false) {
        die( print_r( sqlsrv_errors(), true) );
    }

    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
          echo $row['Firma'].", ".$row['Strasse']."<br />";
    }

    sqlsrv_free_stmt( $stmt);

The problem I am facing is that when I use German vowels like Ö,Ä,Ü,ß in where clause, the query does not return me any data. But without that I get the data. Kindly let me know if how to solve the issue.

Regards

When connecting the SQL server, always set the encoding to UTF-8. Also, save your script as UTF-8.

MSDN example for setting connection encoding: http://msdn.microsoft.com/en-us/library/cc626307.aspx

Or another one : http://www.johnro.net/2012/07/25/php-sqlsrv-problems-with-utf-8-values-set-your-characterset-option/

You should specify the collation when creating tables for the database search. As a local fix for now, you can just change the collation for the relevant table (or only its "value" column) to "utf8_bin" and it will work :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM