I am trying to make an article poster that works perfectly until I put special characters in the html form (like ;,! etc.). I Googled it and found something about the table collation (which is utf8_unicode_ci by default).
I have <meta charset="utf-8"> into the header file and mysqli_set_charset($conn, 'utf8') after connection to the database. Also the form has accept-charset="utf-8" attribute.
Here is what happens after sending the form:
if(isset($_POST['sendForm']))
{
$articleTitle = $_POST['title'];
$articleText = $_POST['text'];
$name = $_SESSION['name'];
$currentDateMySQL = date("Y.m.d");
$sql = "INSERT INTO articles (title, text, owner, date_added) VALUES ('$articleTitle', '$articleText', '$name', '$currentDateMySQL')";
$result = mysqli_query($conn, $sql);
if($result === false)
{
$color = "red";
$infoText = "Could not insert your information into the database. Error number: <b>" . mysqli_errno($conn) . "</b>. :( Try again.";
}
else
{
$color = "green";
$infoText = "Succesfully writen the article into the database. :)";
}
}
Also the given error number is 1064. There is no error in the SQL code, it works perfectly without special characters.
You need do escape every input you trying to insert into a database otherwise you risking sql-injection attacks:
$articleText = mysql_real_escape_string($articleText);
Also you shouldn't use native sql directly anymore, it is deprecated. You should use prepared statements instead.
If you changed your table collation after creation, it does not mean your column collation does match.
All of the following charsets should match so that your data is inserted correctly:
column charset collation
connection charset
Even better, to have the same charset everywhere:
defaut charset
database charset
table charset
column charset
connection charset
Related
I want to store special characters in oracle sql, like á, é, ő etc. How can i do it? Every character turns into ?? when i select them. Never used oracle sql its a real pain in the ass for me.
PHP code:
$id = $_POST['id'];
$comment = $_POST['comment-content'];
$username = $_SESSION['username'];
$q = "INSERT INTO hozzaszolasok (username, image_id, content, date_added) VALUES ('$username',$id,'$comment',(SELECT sysdate FROM dual))";
$s = oci_parse($c, $q);
oci_execute($s);
oci_free_statement($s);
oci_close($c);
echo '<script>window.location.href = "comments.php?id=' . $id . '";</script>';
When you open the connection to Oracle DB, specify the character set parameter as AL32UTF8, see the oci_connect() documentation:
$c = oci_connect($username, $password, $connection_string, 'AL32UTF8');
Some of the older globalization information in The Underground PHP and Oracle Manual may still be relevant.
Review the globalization recommendations for your PHP version to make sure your HTML pages are sending characters to the browser correctly.
I get a text from a database in UTF-8 using JSON and it is in a file right now. When I print the data it is:
توحید در نگاه امام علی (علیه السلام)
But when I insert it in database I get this error:
Insertion Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '�وحید در نگاه امام علی ' at line 1
This is my code
<?php
header('Content-Type: application/json; charset=utf-8');
$con = mysql_connect("localhost","bbbbbbb","bbbbbbb");
mysql_select_db("db", $con);
if(file_exists('./json/file.json')){
$jsondata = file_get_contents('./json/file.json');
$data = json_decode($jsondata, true);
foreach ($data['nodes'] as $node){
$data_element = $node['node'];
$title = $data_element['title'];
$summary = $data_element['summary'];
$body = $data_element['body'];
$id = $data_element['id'];
print $title."\n";
$insert = "INSERT INTO main(title) VALUES ($title)";
mysql_query($insert) or die("Insertion Error: ". mysql_error());
}
}
else
print "File doesn't exist";
The database and its columns are all utf8_general_ci. Why the printed text is not same as the one that is inserted in Database?
The database-connection needs to know it is utf8 too. mysql_set_charset('utf8').
Instead of the deprecated mysql_con, you should have a look at mysqli.
use this
$mysqli = new mysqli("localhost","root","","your_db_name");
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");
Although #ericwenn and #SahilManchal bring up valid points, the immediate error is the failure to quote the string in ... VALUES ($title) ....
Do not blindly put a string into the query.
This will work but it is not safe: ... VALUES ('$title') .... It is subject to a hack called "sql injection". See real_escape_string.
Also, after connecting with mysqli, use
$mysqli->set_charset('utf8');
That is preferable to SET NAMES.
I have some text in a database. I use French and English. French has accents, and some special characters like ç. I use Mamp, MySQL and PHP.
I have collation latin1_swedish-ci (the default). I tried utf8_general_ci and the result is the same.
If I use in a html page, I have this in the head: <meta charset="UTF-8">
As an example, in the database I have "voilà".
When I echo the text from the database to html:
$con = mysqli_connect("localhost","root","root");
if (!$con) {
die('The connexion failed: ' . mysqli_error());
}
if (!mysqli_select_db($con, 'prova')){
echo "Connection with database was not possible";
}
$result = mysqli_query($con, "SELECT * FROM test1
WHERE id='1' ")
or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$text = $row['first'];
echo $text; //I see: voil�
echo htmlentities($text); //I see nothing
echo utf8_encode($text); //This works: I see voilà
}
Why htmlentities does not work?
Is utf8_encode(); the way to go? I have to use that always when I output something from the database? Why do I have to use that if the collation is already UTF8? Is there any better way to store and output text with accents in a MySQL database?
After you connect to the DB you should set the client charset to UTF8:
mysqli_set_charset($con, "UTF8");
Otherwise the mysql client transforms the UTF8 'voilà' to latin1 ('cause it seems that is it's default).
Either you tell the client that I want everything in UTF8, or you get it with the default latin1, and convert it one-by-one yourself calling utf8_encose($text)
I am creating my website with HTML and PHP with a post function. In the post, I am using special special characters (Å, Á ...), but they appear as � on the screen. However, all of the HTML content works.
Any idea?
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
getPosts();
?>
And the Functions file:
<?php
include('connect.php');
function getPosts() {
$query = mysql_query("SELECT * FROM posts") or die(mysql_error());
while($post = mysql_fetch_assoc($query)) {
echo "<h2>" . $post['Title'] . " by " . $post['Author'] . "</h2>";
echo $post['Content'];
}
}
?>
Make sure your MySQL character set and collation (at least for this database/table/column) is utf8.
Also make sure that you set the connection charset correctly:
mysql_set_charset ( "utf8" );
This requires PHP 5.2.3 and MySQL 5.0.7. Also consider switching to MySQLi or PDO which usually handles this better. The obsolete mysql_* API has been deprecated in PHP 5.5
chances are your mySQL database table is not in the correct collation.
ALTER TABLE `posts` CHANGE `content` `content` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ;
do a change like this for your entire database, table, or cell.
I am learning PHP programming, so I have setup testing database and try to do various things with it. So situation is like that:
Database collation is utf8_general_ci.
There is table "books" created by query
create table books
( isbn char(13) not null primary key,
author char(50),
title char(100),
price float(4,2)
);
Then it is filled with some sample data - note that text entries are in russian. This query is saved as utf-8 without BOM .sql and executed.
insert into books values
("5-8459-0046-8", "Майкл Морган", "Java 2. Руководство разработчика", 34.99),
("5-8459-1082-X", "Кристофер Негус", "Linux. Библия пользователя", 24.99),
("5-8459-1134-6", "Марина Смолина", "CorelDRAW X3. Самоучитель", 24.99),
("5-8459-0426-9", "Родерик Смит", "Сетевые средства Linux", 49.99);
When I review contents of created table via phpMyAdmin, I get correct results.
When I retrieve data from this table and try to display it via php, I get question marks instead of russian symbols. Here is piece of my php code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Books</title>
</head>
<body>
<?php
header("Content-type: text/html; charset=utf-8");
mysqli_set_charset('utf8');
# $db = new mysqli('localhost', 'login', 'password', 'database');
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
for ($i = 0; $i < $num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i+1).". Title: ";
echo htmlspecialchars (stripslashes($row['title']));
echo "</strong><br />Author: ";
echo stripslashes($row['author']);
echo "<br />ISBN: ";
echo stripslashes($row['isbn']);
echo "<br />Price: ";
echo stripslashes($row['price']);
echo "</p>";
}
...
And here is the output:
1. Название: Java 2. ??????????? ????????????
Автор: ????? ??????
ISBN: 5-8459-0046-8
Цена: 34.99
Can someone point out what I am doing wrong?
Can someone point out what I am doing wrong?
Yes, I can.
You didn't tell Mysql server, what data encoding you want.
Mysql can supply any encoding in case your page encoding is different from stored data encoding. And recode it on the fly.
Thus, it needs to be told of client's preferred encoding (your PHP code being that database client).
By default it's latin1. Thus, because there is no such symbols in the latin1 character table, question marks being returned instead.
There are 2 ways to tell mysql what encoding we want:
a slightly more preferred one is mysqli_set_charset() function (method in your case).
less preferred one is SET NAMES query.
But as long as you are using mysqli extension properly, doesn't really matter. (though you aren't)
Note that in mysql this encoding is called utf8, without dashes or spaces.
Try to set output charset:
SET NAMES 'utf-8'
SET CHARACTER SET utf-8
Create .htaccess file:
AddDefaultCharset utf-8
AddCharset utf-8 *
CharsetSourceEnc utf-8
CharsetDefault utf-8
Save files in UTF-8 without BOM.
Set charset in html head.
After your mysql_connect, set your connection to UTF-8 :
mysql_query("SET NAMES utf8");
Follow Alexander advices for .htaccess, header and files encoding
You probably need to call mysqli_set_charset('utf8'); after you set up your connection with new mysqli(...) as it works on a link rather than a global setting.
so..
# $db = new mysqli('localhost', 'login', 'password', 'database');
mysqli_set_charset($db, 'utf8');
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
By the way, that query seems to be open to SQL-injection unless $searchterm is sanitized. Just something to keep in mind, consider using prepared statements.
And using # to suppress errors is generally not recommended, especially not during development. Better to deal with error-conditions.
after your mysql_query add
#mysql_query("SET character_set_server='utf8'; ");
#mysql_query("SET character_set_client='utf8'; ");
#mysql_query("SET character_set_results='utf8'; ");
#mysql_query("SET character_set_connection='utf8'; ");
#mysql_query("SET character_set_database='utf8'; ");
#mysql_query("SET collation_connection='utf8_general_ci'; ");
#mysql_query("SET collation_database='utf8_general_ci'; ");
#mysql_query("SET collation_server='utf8_general_ci'; ");
Try to put also in the HTML document Head the meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
this is different to the HTTP header header("Content-type: text/html; charset=utf-8");