Data formatting error

0

Well it's as follows.

I'm going to get certain texts from the database, then I'll list them in the web, and when I do, the accents are all square and so on. But when I write in the code with accents and such, everything is normal. And I'm using the meta tag and anyway nothing.

What's the problem?

    
asked by anonymous 09.06.2015 / 01:34

2 answers

0

The problem is in the coding between the written code and the database. You can follow some steps to solve, I'll illustrate with UTF-8:

First make sure your IDE is encoding in UTF-8. In case Eclipse is in the Edit > > Set Encoding > > Switch to UTF-8 .

Second, make sure your browser understands that you are using UTF-8:

<?php
header('Content-Type: text/html; charset=UTF-8');

...

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="UTF-8" >

Make sure your bank is UTF-8 encoded:

  

MySQL

CREATE DATABASE nome_bd CHARACTER SET UTF8;

And that your database access is in the same encoding.

  

MySQL (PDO):

$dsn = "mysql:host=localhost;dbname=world;charset=utf8";
$opcoes = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'
);
$pdo = new PDO($dsn, $usuario, $senha, $opcoes);
  

MySQLi:

$mysqli = new mysqli(...);
$mysqli->set_charset('utf8'));
    
09.06.2015 / 04:18
0

As you have already created the database you will have a slightly larger job, do the following:

  

Change the database encoding:

ALTER DATABASE <nome_do_banco> CHARACTER SET utf8 COLLATE utf8_unicode_ci;
  

Change the encoding of tables:

ALTER TABLE <nome_da_tabela> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
  

Change the encoding of the columns:

ALTER TABLE <nome_da_tabela> MODIFY <nome_da_coluna> <VARCHAR(255)> CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Or you can run these queries down to make it easier for you to work around the bank.

SELECT DISTINCT concat('ALTER DATABASE ', TABLE_SCHEMA, ' CHARACTER SET utf8 COLLATE utf8_unicode_ci;')
from information_schema.tables
where TABLE_SCHEMA like  'database_name';

-

SELECT concat('ALTER TABLE ', TABLE_SCHEMA, '.', table_name, ' CHARACTER SET utf8 COLLATE utf8_unicode_ci;')
from information_schema.tables
where TABLE_SCHEMA like 'database_name';

-

SELECT concat('ALTER TABLE ', t1.TABLE_SCHEMA, '.', t1.table_name, ' MODIFY ', t1.column_name, ' ', t1.data_type , '(' , CHARACTER_MAXIMUM_LENGTH , ')' , ' CHARACTER SET utf8 COLLATE utf8_unicode_ci;')
from information_schema.columns t1
where t1.TABLE_SCHEMA like 'database_name' and CHARACTER_SET_NAME = ‘old_charset_name';
    
09.06.2015 / 20:56