如何在PHP中打印ñ

In the database, there's a name containing ñ (ex Niño)

when I retrieve it from the database then echo it, the output will be Ni�o.

How can I fix this?

You will have to make sure that your data and the document you output (via PHP) is of the same encoding.

For instance if your data in the database is UTF8 (and not corrupted). You should make sure to set UTF-8 in the header of your document. Like this:

header('Content-Type: text/html; charset=utf-8');

(Be sure to set your headers before you output any content.)

Sometimes you have to tell your DB that you use a specific character-set:

$db->query("SET NAMES 'utf8'");
$db->query("SET CHARACTER SET utf8");

This article describes how to ensure that PHP and DB are speaking the same "language" from beginning to end. It entails that you use the mb* methods in stead of the regular ones.

I would suggest using a library for these sort of things - like Flourishlib that handles all the messy stuff for you - the db, the headers, etc.

Lastly have a look at this general UTF 8 article for some insights into the problems.

did you check the database configuration??check it first on your database. i have that same problem before when i used cakephp.. this is what it looks like in cakephp

//cakephp database configuration

<?php
/**
 1. This is core configuration file.
 2.  3. Use it to configure core behaviour ofCake.
 4.  5. PHP versions 4 and 5
 6.  7. CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 8. Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 9.  10. Licensed under The MIT License
 11. Redistributions of files must retain the above copyright notice.
 12.  13. @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14. @link          http://cakephp.org CakePHP(tm) Project
 15. @package       cake
 16. @subpackage    cake.app.config
 17. @since         CakePHP(tm) v 0.2.9
 18. @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
/**
 19. In this file you set up your database connection details.
 20.  21. @package       cake
 22. @subpackage    cake.config
 */
/**
 23. Database configuration class.
 24. You can specify multiple configurations for production, development and testing.
 25.  26. driver => The name of a supported driver; valid options are as follows:
 27.    mysql       - MySQL 4 & 5,
 28.    mysqli      - MySQL 4 & 5 Improved Interface (PHP5 only),
 29.    sqlite      - SQLite (PHP5 only),
 30.    postgres    - PostgreSQL 7 and higher,
 31.    mssql       - Microsoft SQL Server 2000 and higher,
 32.    db2         - IBM DB2, Cloudscape, and Apache Derby (http://php.net/ibm-db2)
 33.    oracle      - Oracle 8 and higher
 34.    firebird    - Firebird/Interbase
 35.    sybase      - Sybase ASE
 36.    adodb-[drivername]  - ADOdb interface wrapper (see below),
 37.    odbc        - ODBC DBO driver
 38.  39. You can add custom database drivers (or override existing drivers) by adding the
 40. appropriate file to app/models/datasources/dbo.  Drivers should be named 'dbo_x.php',
 41. where 'x' is the name of the database.
 42.  43. persistent => true / false
 44. Determines whether or not the database should use a persistent connection
 45.  46. connect =>
 47. ADOdb set the connect to one of these
 48. (http://phplens.com/adodb/supported.databases.html) and
 49. append it '|p' for persistent connection. (mssql|p for example, or just mssql for not persistent)
 50. For all other databases, this setting is deprecated.
 51.  52. host =>
 53. the host you connect to the database.  To add a socket or port number, use 'port' => #
 54.  55. prefix =>
 56. Uses the given prefix for all the tables in this database.  This setting can be overridden
 57. on a per-table basis with the Model::$tablePrefix property.
 58.  59. schema =>
 60. For Postgres and DB2, specifies which schema you would like to use the tables in. Postgres defaults to
 61. 'public', DB2 defaults to empty.
 62.  63. encoding =>
 64. For MySQL, MySQLi, Postgres and DB2, specifies the character encoding to use when connecting to the
 65. database.  Uses database default.
 66.  */
class DATABASE_CONFIG {

var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'web_user',
    'prefix' => '',
    //'encoding' => 'utf8',
);

var $test = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'user',
    'password' => 'password',
    'database' => 'test_database_name',
    'prefix' => '',
    //'encoding' => 'utf8',
);
}

You should uncomment the line that have the encoding =>'utf8'

Try string replace function (str_replace(find,replace,string)) Try To visit this site for more info. http://www.w3schools.com/php/func_string_str_replace.asp Good Luck! Hope this helps you! :-)