Jump to content


Photo

htmlspecialchars


  • Please log in to reply
9 replies to this topic

#1 Bogey

Bogey

    Member

  • Members
  • PipPip
  • 218 posts
  • Gender:Male
  • Location:the Netherlands
  • Languages:html, css, js, php, sql

Posted 10 April 2012 - 06:03 PM

Hi all,

I have this in a cell in my mysql-database: 'René'

When I extract it from SQL with PHP and show it in my HTML page, then I dont get the é!
I get a square with a questionmark in it...

How can I manage it, so it shows the é?

I tried it with htmlspecialchars, but did not get it right :S


Thnx

#2 justsomeguy

justsomeguy

    More Human Than Human

  • Moderator
  • PipPipPipPipPipPipPip
  • 27,031 posts
  • Gender:Male
  • Location:Phoenix
  • Languages:Focusing on PHP and JavaScript

Posted 10 April 2012 - 06:25 PM

The PHP files need to be saved using UTF-8, all pages involved need to have a content-type of UTF-8, and the database needs to use a collation of UTF-8.
Know your history: Babbage | Lovelace | Turing | Hopper | Ritchie
ConTEXT Sublime Text Opera PHP MySQL phpMyAdmin
Use a debugger: Firefox, IE, Chrome, Safari, or Opera
Know the foundations of computer science: algorithms, machine architectures, data structures, etc. Don't just blindly copy techniques from application to application. Know what you are doing, that it works, and why it works. Don't think you know what the industry will be in five years time or what you'll be doing then, so gather a portfolio of general and useful skills. Try to write better, more principled code. Work to make "programming" more of a professional activity and less of a low-level "hacking" activity (programming is also a craft, but not just a craft). Learn from the classics in the field and the better advanced textbooks; don't be satisfied with the easily digested "how to" guides and online documentation - it's shallow.
-- Bjarne Stroustrup

He that teaches himself has a fool for a master.
-- Benjamin Franklin (paraphrased)

#3 boen_robot

boen_robot

    XSLT senior

  • Moderator
  • PipPipPipPipPipPipPip
  • 8,486 posts
  • Gender:Male
  • Location:europe://Bulgaria/Plovdiv
  • Interests:Everything having to do with computers... and science.
  • Languages:(X)HTML, CSS, XML, XSLT, Schema, PHP, JavaScript (a little), other XML based...

Posted 11 April 2012 - 08:42 PM

... and the MySQL connection needs to specify UTF-8 as a charset with mysqli::set_charset(), and if you use htmlspecialchars (as the title of the topic implies), you must specify that in the third argument of htmlspecialchars.
The greatest difficulty in programming is not in finding answers, but in asking yourself the right questions. -- If nobody has said it before, then I'd like to take credit of thinking this up (during summer of 2010).
"Complex problems often have the simplest solutions" -- Not sure who said that first.
=== My projects (all feedback welcomed) ===
XML_XSLT2Processor(0.5.3) - perform XSLT 2.0 transformations in PHP.
PEAR2_Net_Transmitter(1.0.0a4) - reliable sockets.
PEAR2_Cache_SHM(0.1.2) - persistent data storage wrapper.
=== Useful tools ===
NetBeans - full featured PHP IDE, as well as a decent code editor for other things.
Fiddler2 - The best free HTTP debugger. Performance tuning, security check, integrity check, custom requests and more, all made easy.
Gobby - That's NOT my Nickname! Look at the topic.

#4 Bogey

Bogey

    Member

  • Members
  • PipPip
  • 218 posts
  • Gender:Male
  • Location:the Netherlands
  • Languages:html, css, js, php, sql

Posted 12 April 2012 - 07:28 PM

Ok, this I have now:
php-files saved UTF-8;
the particular row in the database has collation '

utf8_general_ci

';

this is in my code:
include($_SERVER['DOCUMENT_ROOT'].'/config.php');
$link = mysqli_connect('localhost',$GLOBALS['dbuser'],$GLOBALS['dbpass']) or die(mysql_error());
mysqli_set_charset($link,"utf8");
 
<H2><?php echo htmlspecialchars($row['naam'],ENT_QUOTES,'UTF-8');?></H2>

It has to echo René, but it echoes Ren

Edited by Bogey, 12 April 2012 - 07:29 PM.


#5 boen_robot

boen_robot

    XSLT senior

  • Moderator
  • PipPipPipPipPipPipPip
  • 8,486 posts
  • Gender:Male
  • Location:europe://Bulgaria/Plovdiv
  • Interests:Everything having to do with computers... and science.
  • Languages:(X)HTML, CSS, XML, XSLT, Schema, PHP, JavaScript (a little), other XML based...

Posted 12 April 2012 - 07:33 PM

Also add
header('Content-Type: text/html;charset=UTF-8');
Somewhere at the top (before any echoes). That's the "all pages involved need to have a content-type of UTF-8" part.
The greatest difficulty in programming is not in finding answers, but in asking yourself the right questions. -- If nobody has said it before, then I'd like to take credit of thinking this up (during summer of 2010).
"Complex problems often have the simplest solutions" -- Not sure who said that first.
=== My projects (all feedback welcomed) ===
XML_XSLT2Processor(0.5.3) - perform XSLT 2.0 transformations in PHP.
PEAR2_Net_Transmitter(1.0.0a4) - reliable sockets.
PEAR2_Cache_SHM(0.1.2) - persistent data storage wrapper.
=== Useful tools ===
NetBeans - full featured PHP IDE, as well as a decent code editor for other things.
Fiddler2 - The best free HTTP debugger. Performance tuning, security check, integrity check, custom requests and more, all made easy.
Gobby - That's NOT my Nickname! Look at the topic.

#6 Bogey

Bogey

    Member

  • Members
  • PipPip
  • 218 posts
  • Gender:Male
  • Location:the Netherlands
  • Languages:html, css, js, php, sql

Posted 14 April 2012 - 05:58 PM

Can't get it done! :S

In database cell is filled: 'René'
Cell has a coallition "utf8_general_ci"

Piece of template file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE>{CMS:TITLE}</TITLE>
<META http-equiv="X-UA-Compatible" content="IE=8" />
<META http-equiv="Content-Type" content="text/html; charset=utf-8" />
<META http-equiv="Content-Language" content="nl" />
<META name="description" content="{CMS:DESCRIPTION}" />
<META name="keywords" content="{CMS:KEYWORDS}" />
{CMS:BASETAG}

Piece of code in php:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/config.php'); //Include de config met username en passwords
//mysql_connect('localhost',$GLOBALS['dbuser'],$GLOBALS['dbpass']) or die(mysql_error());
$link = mysqli_connect('localhost',$GLOBALS['dbuser'],$GLOBALS['dbpass']) or die(mysql_error());
//mysql_select_db($GLOBALS['dbname']);
 
//mysqli_connect('localhost',$GLOBALS['dbuser'],$GLOBALS['dbpass']) or die(mysql_error());
// mysqli_select_db($GLOBALS['dbname']);
// mysqli_set_charset($link, "utf8");
// mysqli_set_charset($link,"utf8");
// mysqli::set_charset('utf8');
 
mysqli_set_charset($link, "utf8")
header ('Content-Type: text/html;charset=UTF-8');
?>
<DIV class='inputContainerLabel'>
<LABEL for='selectNaam'>Naam:</LABEL>
<SELECT style='width:100px;' id='selectNaam' name='selectNaam' onchange="checkAllSelected_Omschrijving()">
<OPTION value=empty>selecteer:</OPTION>
<?php
$result = mysql_query("SELECT * FROM opmerkingen_users ORDER by naam");
$options="";
while ($row= mysql_fetch_array($result)) {
$options.='<OPTION value ="' . htmlspecialchars($row['naam'],ENT_QUOTES,'UTF-8') . '">' . htmlspecialchars($row['naam'],ENT_QUOTES,'UTF-8').'</OPTION>';
}
echo $options;
?>
</SELECT>
</DIV>

I tried different options, but can't get it done :S

What do I wrong, or what am I missing?

Edited by Bogey, 14 April 2012 - 06:00 PM.


#7 Bogey

Bogey

    Member

  • Members
  • PipPip
  • 218 posts
  • Gender:Male
  • Location:the Netherlands
  • Languages:html, css, js, php, sql

Posted 14 April 2012 - 06:01 PM

When I use mysql, then I get the info from DB, but not the right way... looking at é!

When I use mysqli, I cant get info from DB.... :S

When use mysqli, then also use mysqli_query? or isn't that a existing command?

#8 boen_robot

boen_robot

    XSLT senior

  • Moderator
  • PipPipPipPipPipPipPip
  • 8,486 posts
  • Gender:Male
  • Location:europe://Bulgaria/Plovdiv
  • Interests:Everything having to do with computers... and science.
  • Languages:(X)HTML, CSS, XML, XSLT, Schema, PHP, JavaScript (a little), other XML based...

Posted 14 April 2012 - 06:39 PM

Wait... what's with this template thing?

The line
header('Content-Type: text/html;charset=UTF-8');
needs to appear before this template outputs anything... ideally, even before you call the template engine.

Plus, this template system itself might need a setting somewhere in it too, leading to the mess up in the absence of that setting.

To ensure it's not the template system's fault, just for the sake of testing, run only the "Piece of code in php" part.


As for MySQLi... I see a syntax error here:
mysqli_set_charset($link, "utf8")
you're missing the semicolon.

Also, you haven't replaced all MySQL functions with the MySQLi equivalents, and finally, you do need
mysqli_select_db($GLOBALS['dbname']);

The greatest difficulty in programming is not in finding answers, but in asking yourself the right questions. -- If nobody has said it before, then I'd like to take credit of thinking this up (during summer of 2010).
"Complex problems often have the simplest solutions" -- Not sure who said that first.
=== My projects (all feedback welcomed) ===
XML_XSLT2Processor(0.5.3) - perform XSLT 2.0 transformations in PHP.
PEAR2_Net_Transmitter(1.0.0a4) - reliable sockets.
PEAR2_Cache_SHM(0.1.2) - persistent data storage wrapper.
=== Useful tools ===
NetBeans - full featured PHP IDE, as well as a decent code editor for other things.
Fiddler2 - The best free HTTP debugger. Performance tuning, security check, integrity check, custom requests and more, all made easy.
Gobby - That's NOT my Nickname! Look at the topic.

#9 Bogey

Bogey

    Member

  • Members
  • PipPip
  • 218 posts
  • Gender:Male
  • Location:the Netherlands
  • Languages:html, css, js, php, sql

Posted 15 April 2012 - 07:17 AM

when I run this code in a new php file, and just browse to that single php file, then it all goed well...
<?php
include($_SERVER['DOCUMENT_ROOT'].'/config.php'); //Include de config met username en passwords
mysql_connect('localhost',$GLOBALS['dbuser'],$GLOBALS['dbpass']) or die(mysql_error());
mysql_select_db($GLOBALS['dbname']);
?>
<DIV class='inputContainerLabel'>
<LABEL for='selectNaam'>Naam:</LABEL>
<SELECT style='width:100px;' id='selectNaam' name='selectNaam' onchange="checkAllSelected_Omschrijving()">
<OPTION value=empty>selecteer:</OPTION>
<?php
$result = mysql_query("SELECT * FROM opmerkingen_users ORDER by naam");
$options="";
while ($row= mysql_fetch_array($result)) {
$options.='<OPTION value ="' . $row['naam'] . '">' . $row['naam'].'</OPTION>';
}
echo $options;
?>
</SELECT>
</DIV>

When I call that file in the template, then its not going fine :S

So it has to be something with the template... I go look further on that one ;)

#10 Bogey

Bogey

    Member

  • Members
  • PipPip
  • 218 posts
  • Gender:Male
  • Location:the Netherlands
  • Languages:html, css, js, php, sql

Posted 15 April 2012 - 07:32 AM

Think it hasn't be something with template, but has something to do with the CMS....
I just emailed my friend, who has made the CMS, hope he knows the sollution ;)

I come back to this one ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users