4 votes

special characters

Hi, today I come with a small problem that is this

 echo $consulta;
 

to which he responds with:

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

devido to the character or that 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 throws me

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

5voto

Eduardo Munizaga Points 1876

It would be good if you could incorporate how you connect as if you're using PDO you should do it like this:

$pdo = new Conexion();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("SET CHARACTER SET utf8"); // <--utf8

In case you do the former would be of this form:

$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);
mysql_select_db($db,$conexion);

with mysqli would be

<?PHP

@ $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()";
$db->set_charset('utf8');

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

?>

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

2voto

Elenasys Points 67941

Several options for this problem, apparently you tried

a) with the method utf8_encode()

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

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

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

0voto

Edgar Conrado Points 381

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

 $connect = new PDO(
    "mysql:host=$host;dbname=$db",
    $user,
    $pass,
    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
 

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X