Special PHP characters


Good morning, today I come with a small problem that is this

echo $consulta;

to which he replies with:

SELECT SUM(carrito_cant) as cantidad FROM carrito_tb WHERE carrito_folio='3041M�Ra' and carrito_NP='NA';

Devido to the character or who exchanges it for already try with

$consulta = utf8_encode ( $consulta );

also with these two:

header('Content-type: text/html; charset=utf-8');
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

but no doubt no one throws me

SELECT SUM(carrito_cant) as cantidad FROM carrito_tb WHERE carrito_folio='3041MóRa' and carrito_NP='NA';
asked by Ing. Marquez Adam 10.02.2016 в 00:27

3 answers


It would be nice if you could incorporate your way of connecting, because if you use PDO you should do it like this:

$pdo = new Conexion();
$pdo->exec("SET CHARACTER SET utf8"); // <--utf8

In case you do it the old way it would be like this:

$conexion = mysql_connect($dbhost, $dbusuario, $dbpassword);
mysql_query("SET character_set_results = 'utf8', character_set_client ='utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conexion);

with mysqli would be


@ $db = new mysqli(localhost, "root", "", "biblioteca");

if ($db->connect_error)
    die('Error de Conexion ('.$db->connect_errno.')'.$db->connect_error);

echo "Conexion correcta con la base de datos...".$db->host_info;
echo "Utilizando un charset, por defecto, de tipo: ".$db->character_set_name()";

echo "Utilizando un nuevo charset de tipo: ".$db->character_set_name();


And of course check if the database was created with character = utf8 and the collation = utf8_general_ci as well as the specific table to which queries.

answered by 10.02.2016 / 00:37

Several options for this problem, apparently you tried

a) with the utf8_encode() method

b) Add the header with the charset for utf-8 :

<?php header('content-type: text/html; charset=utf-8');

I recommend as a third option, save your file php with utf-8 .

answered by 10.02.2016 в 06:13

My problem was solved using this command when connecting to the database:

$connect = new PDO(
answered by 05.12.2016 в 23:52