How to Insert Special Character in MySQL Via PHP and Display on HTML Page

how to insert special character in mysql via php and display on html page

Just simply add those symbols to your text, and execute it as SQL query:

INSERT INTO tbl_name VALUES ("Here's my text: ©®");

When you want to display it one the website don't do anything with these symbols (but remember to escape at least <, >, & (using htmlspecialchars()) cause those has special meaning in XML/SGML (HTML) documents)

PS. Also remember to escape text passed to SQL query using mysql_real_escape_string() to avoid any SQL Injection problems. If your server has magic_quotes_gpc enabled disable it or at least filter your GET/POST/COOKIE data to its raw value. You should always consciously escape values.

EDIT:

According to your comment... I don't remember whether magic_quotes_gpc are enabled by default but you can easily undone magic quotes effect. Just on the very beginning of your PHP code add something like this:

if (get_magic_quotes_gpc()) {
array_walk_recursive($_GET, 'stripslashes');
array_walk_recursive($_POST, 'stripslashes');
array_walk_recursive($_COOKIE, 'stripslashes');
}

Now each GPC value should be always raw - without quotes - so you have to escape it manually before passing any variable into query.

How to display special characters from MySQL database to html?

Try using SET NAMES utf8 after connecting to MySQL:

$db1 = new db_mysql($conf['db_hostname'], $conf['db_username'], 
$conf['db_password'], $conf['db_name']);

$db1->query("SET NAMES utf8");

As the manual says:

SET NAMES indicates what character set the client will use to send SQL
statements to the server... It also specifies the character set that the server should
use for sending results back to the client.

How to insert special character like £ with price like (£ 2,000) in mysql database

Try encoding the table as:

ALTER TABLE <table_name> CONVERT TO
CHARACTER SET utf8
COLLATE utf8_general_ci;

And make sure the datatype of the column is varchar.

Also if you plan to output that data in a webpage you can to insert HTML Entities for Currencies in the db and then when you output it in a webpage, the HTML will render the entities as regular characters.

It works for me

Special Characters into MySQL using PHP

You are not using mysqli properly. (Not your fault, since the PHP manual doesn't explain it well enough)

To open the connection to mysqli you must at least do the following:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // enable error reporting
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');

set_charset() method ensures that the data sent to the DB is using the correct character set. Without it the data might get mangled up.

You also must enable proper mysqli error reporting with this line:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Without it you would need to use mysqli_error($conn) to check for errors, which could leak sensitive information to the users if misused.

Do not use utf8_encode(). Most of the time you do not need this function. Unless you really know what you are doing, get rid of it.

Warning:

utf8_general_ci is a very old and obsolete charset. If you are using MySQL 5.5.3 or later or MariaDB you should be using utf8mb4 charset. See What is the difference between utf8mb4 and utf8 charsets in MySQL?

Saving and Displaying HTML and special characters in a mysql database safely?

While encoding characters is a good thing, one must make sure not to over-encode.

Only encode what /needs/ encoded at that time. Don't encode the HTML before putting it into your database. You may want to print things out later, or you may want to run searches against it. Use the proper escape sequences for SQL (or, better yet, use PDO).

Only when you are sending things to the browser should you escape the HTML, and then you need to decide what kind of escaping you need. To convert things like < and & as the character entities so they will display properly, then use the right escape method for that.

Special characters showing up on content retrieved from mysql using php

Try setting the utf8 charset at the PDO Connection like this:

$pdo = new PDO(DSN, USER, PASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));



Related Topics



Leave a reply



Submit