I have an sql database with sql_latin1_general_cp1_ci_as collation.
the database will contain about 3 language (English,Turkish,Arabic).
At the main page I set the charset to utf-8 as:
header('Content-Type: text/html; charset=UTF-8');
<meta charset="utf-8">
And I use this code in my php page:
$sql_server = "server";
$sql_user = "user";
$sql_password = "password";
$sql_database = "database";
$sql_conn_string = 'Driver={SQL Server};Server='.$sql_server.';Database='.$sql_database.';';
if ($sql_conn = odbc_connect($sql_conn_string, $sql_user, $sql_password)){
echo 'Connected';
$name = $_POST['cust_name'];
if (odbc_exec($sql_conn,"INSERT INTO inv01 (cust) VALUES (N'".$name."')")){
echo 'is inserted';
}
else{
echo 'is not inserted';
}
odbc_close($sql_conn);
}
else{
echo 'Connection Error';
}
When I enter an English words there is no problem
But when I use the other languages the words is inserted as unclear characters
I tried to use iconv but it's not working
$name = iconv("UTF_8","Windows-1252",$name);
//and
$name = iconv("UTF_8","UCS-2LE",$name);
What is the best collation in my case if sql_latin1_general_cp1_ci_as is not, and is there another solution without change the collation
I hope that my question is not repeated because I have read many questions here and I have not found a correct answer.
Thanks.
You can try with this:
Ensure that your 'cust' column is nvarchar().
Encode your html and php files in UTF-8 (I usually use Notepad++ for this step).
Convert input data.
Code (based on your example):
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta charset="utf-8">
<?php
...
$name = $_POST['cust_name'];
$name = iconv('UTF-8', 'UTF-16LE', $name);
$name = bin2hex($name);
$sql = "INSERT INTO inv01 (cust) VALUES (CONVERT(nvarchar(MAX), 0x".$value."))";
...
?>
</head>
<body></body>
</html>
Related
I am having issues with retrieving and processing data that is in Russian using the cyrillic character set.
I get the data in a text file from an FTP server with the code below and it displays every character with the black diamonds with question marks inside.
If I view it directly by accessing the FTP address with the browser, it displays correctly.
I have tried changing this line:
to
and
and while I get different results, none show the same as when accessing the file directly by the browser.
I'm not sure how to get the code to display the same as the browser when I view it directly
This would be an example of how I view the text file directly which displays correctly : ftp://username:password#ftp.mysite.com/test.txt
This is the code I am using which displays the black diamonds with question marks (other other incorrect characters, depending on the charset mentioned above).
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
$username = "username";
$password = "password";
$server = "ftp://ftp.mysite.com"
$remoteFile = "test.txt";
$conn = ftp_connect($server);
if (#ftp_login($conn, $username, $password)) {
echo "";
}
else {
echo "";
}
ob_start();
ftp_get($conn, 'php://output', $remoteFile, FTP_ASCII);
$data = ob_get_contents();
ob_end_clean();
ftp_close($conn);
echo $data;
?>
</html>
I managed to resolve this by using mb_convert_encoding by adding the following line :
$new_data = mb_convert_encoding($data, "utf-8", "Windows-1251");
with the resulting code as :
<html>
<?php
$username = "username";
$password = "password";
$server = "ftp://ftp.mysite.com"
$remoteFile = "test.txt";
$conn = ftp_connect($server);
if (#ftp_login($conn, $username, $password)) {
echo "";
}
else {
echo "";
}
ob_start();
ftp_get($conn, 'php://output', $remoteFile, FTP_ASCII);
$data = ob_get_contents();
ob_end_clean();
ftp_close($conn);
$new_data = mb_convert_encoding($data, "utf-8", "Windows-1251");
echo $data;
?>
</html>
Hope this helps someone...
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
My problem is that how my query result unreadable for humans.
I get this result in browser:
ID: 5 Reg.date: 2016-02-29 18:57:52 C�si
And the name should be 'Cósi'.
The php is in UTF-8, the database is in utf8-hungarian-ci.
So I do the query and after it I put the results into a $user array, and I echo the first 3 item like: echo "ID: " . $user["userID"] . "Reg.date: " . $user["regdate"] . $user["name"];
I tried header('Content-type: text/html; charset=UTF-8'); but it neither works.
I have a innoDB phpmyadmin database, but the server is my father's computer. A xampp, should I search the problem there?
Here is the all php:
$con = mysqli_connect("127.0.0.1", "kxxx", "csxxx", "bxxx");
$password = "asd";
$username = "carrie#gmail.com";
$statement = mysqli_prepare($con, "SELECT * FROM user WHERE email = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $reg_date, $name, $email, $password, $phonenumber);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user["userID"] = $userID;
$user["regdate"] = $reg_date;
$user["name"] = $name; / which should be "Cósi"
$user["email"] = $email;
$user["password"] = $password;
$user["phonenumber"] = $phonenumber;
}
echo "ID: " . $user["userID"] . "Reg. dátum: " . $user["regdate"] . $user["name"];
echo json_encode($user, JSON_UNESCAPED_UNICODE);
The json_encode is disappears, if empty [] should displayed, but that's not, only the first 3 item of the array.
So I want the C�si to be Cósi. What should I do? I tried to changed meta tag to charset='utf-8', mysql_query("SET NAMES 'utf8'") and the header thing what I mentioned above, all the columns are utf8-hungarian-ci, all the tables and the database is too.... So maybe the server configurations are bed?
When I insert into the database over a php, in the database the 'Cósi' is displays well, everything saved in the database right.
Thank you guys, I resolved it wtih:
mysqli_set_charset($con, 'utf8mb4');
You need the proper html tags:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Your Page Title</title>
</head>
<body>
YOUR CONTENT HERE
</body>
</html>
Check mb_convert_encoding in php.
$str = mb_convert_encoding($str, "UTF-8", "utf8-hungarian-ci");
This is about retriving the data in form of CSV from Mysql Table : -
Code , I tried :-
<?php
// mysql database connection details
$host = "localhost";
$username = "root";
$password = "hello";
$dbname = "mysql2csv";
// open connection to mysql database
$connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
// fetch mysql table rows
$sql = "select * from tbl_books";
$result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));
$fp = fopen('books.csv', 'w');
while($row = mysqli_fetch_assoc($result))
{
fputcsv($fp, $row);
}
fclose($fp);
//close the db connection
mysqli_close($connection);
?>
Errors Obtained...
04:12:27.093 The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.1 mysql2csv.php.
your help will be appreciated ...
Add those lines to your html header
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
Edit:
If you are using PHP file:
header('Content-Type: text/html; charset=utf-8');
I set utf8 for my PHP code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<?php
header("Content-Type: text/html; charset=UTF-8");
if (isset($_REQUEST['op'])){
$op = $_REQUEST['op'];
} else {
echo "Invalid op";
exit;
}
and also for database connection:
function db_connect(){
$con = mysqli_connect("localhost", "root", "", "ex");
mysqli_set_charset($con, 'utf8');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
return $con;
}
and I saved my records as utf8 characters in my database and set collation as utf8_general and use SET NAMES utf8; in SQL.
but I see this unicode in the browser instead of my real records in db!
what can I do to see my real records as persian letters on the browser?
THANK YOU FOR ANSWERING SOON
i building simple WebService for my web app which contain only two php files connecttodatabase.php contain the code of mysql connection and index.php contain the code of my webService but i got sql 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 'desc, cat, price FROM itmes' at line 3
connecttodatabase.php
<?php
$requesturi = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$pos = strpos($requesturi, "localhost:99/self/index.php");
$hostname = "localhost";
$database = "self";
$username = "root";
$password = "";
$self = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
//get data
require_once('connecttodatabase.php');
mysql_select_db($database, $self);
//build query
$query =
"SELECT
name,
desc,
cat,
price
FROM itmes";
$rsPackages = mysql_query($query, $self) or
die(mysql_error());
$arRows = array();
while ($row_rsPackages = mysql_fetch_assoc($rsPackages)) {
array_push($arRows, $row_rsPackages);
}
header('Content-type: application/json');
echo json_encode($arRows);
?>
</body>
</html>
It is because you used command DESC in selection, which throws an Exception. Use special symbols to prevent these problems:
`something`
So your code will have this form:
$query =
"SELECT
`name`,
`desc`,
`cat`,
`price`
FROM `itmes`";
and it should be working.