Difficulty with Encoding

Asked

Viewed 65 times

0

Good,

I have a file PHP with encoding UTF-8. And the text with accents in the part HTML they look like they should, but when I go to the database.

Encoding of the database

Data from the database

Database data viewed in the file php

include("connection.php");
<?php
   include("connection.php");

   echo "<div class='narrow-block wrapper'>
<h2>Inscrições (Federados)</h2>
<table id='table2'>
   	$result2 = mysqli_query($con,"SELECT * FROM registofederados");
	while($row = mysqli_fetch_array($result2)){
		echo "<tr>";
			echo "<td>" . $row['dorsal'] . "</td>";
			echo "<td>" . $row['nome'] . "</td>";
			echo "<td>" . $row['bi'] . "</td>";
			echo "<td>" . $row['dataNasc'] . "</td>";
			echo "<td>" . $row['n_federado'] . "</td>";
			echo "<td>" . $row['email'] . "</td>";
			echo "<td>" . $row['telemovel'] . "</td>";
			echo "<td>" . $row['morada'] . "</td>";
			echo "<td>" . $row['local_dorsal'] . "</td>";
			echo "<td>" . $row['pagamento'] . "</td>";
			echo "<td>" . $row['equipa'] . "</td>";
			echo "<td>" . $row['categoria'] . "km </td>";
			if($row['almoco'] == 1)
				echo "<td>Sim</td>";
			else
				echo "<td>Não</td>";
			if($row['pago'] == 1)
				echo "<td>Sim</td>";
			else
				echo "<td>Não</td>";
		echo "</tr>";
	}
   	</table>
  </div>";
mysqli_close($con);
?>

  • If you use the Notepad++ and to avoid future problems go to the options menu, click on Format, and selects the encoding UTF-8(Sem BOM), you can set this by default in the settings, so as soon as you create a file it already comes in UTF-8(Sem BOM), if you already have a code written in ANSI you must click on Convert to UTF-8(No GOOD) it converts not being necessary corrections in the accents for example, as it already does it automatically. Remember UTF-8(NO GOOD) without BOM.

1 answer

1


Execute the following command before executing the query:

if( function_exists('mysql_set_charset') ) { 
    mysql_set_charset('utf8', $con); 
} else { 
    mysql_query("SET NAMES 'utf8'", $con); 
}

You can also change the encoding of your database to:

utf8_general_ci
  • i have this but the problem is in the data coming from the database, that the accents come in strange characters as referred and shown above

  • I think I’ve figured out your problem, try to execute the following command before executing the query: mysql_set_charset("utf8");

  • Type like this: mysql_set_charset("utf8"); $result = mysqli_query($con,"SELECT * FROM record Where type = 0");

  • Like this: $Conn = mysql_connect($server, $username, $password); mysql_set_charset("UTF8", $Conn);

  • I’m a little new at this php database. And now you are giving me the following error: Warning: mysqli_query() expects at least 2 Parameters, 1 Given in C: xampp htdocs exercicios Oxygentarget php verregistosA.php on line 211 and the error code is : $result = mysqli_query($con,"SELECT * FROM registration Where type='0'"); what is this?

  • Can you please show how you’re doing? Put your PHP code in the main post to help better.

  • Ready @Hélder is already there some of the code I think is relevant

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.