php unicode UTF-8 result in required files - php

i add this define() function in my config.php file.
$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_',$options['0']['sign']);
Now, i required config.php file into my index.php page like this :
require $abspath .'/config.php';
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?PHP echo _SIGN_; ?>
</body>
</html>
now in output i have this result:
?????????????????????
worked result :
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_',$options['0']['sign']);
<?PHP echo _SIGN_; ?>
</body>
</html>
output result :
تواصل معنا
how do fix this for show unicode UTF-8 when i required config.php ?!

Fist where did you set the variable $abspath ?
Then you have to put <?php and ?> tags around your require statemet and in your config file around your php!
Now the example below works fine for me and is pretty much the same as yours!
config.php:
<?php
$options['0']['sign'] = "تواصل معنا"; //$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_', $options['0']['sign']);
?>
index.php:
<?php require 'config.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?PHP echo _SIGN_; ?>
</body>
</html>
And the Output is:
تواصل معنا

Did you try after establishing connection to the database execute:
set_charset ( "utf8")
I use MySQLi and make it this way:
//-> conncecting to db
$mysqli = new mysqli ( $hostname, $username, $password, $db );
if ( $mysqli -> connect_errno ) {
echo 'Failed to connect to MySQL: ' . $mysqli -> connect_error;
}
// change character set to utf8
if ( ! $mysqli -> set_charset ( "utf8") ) {
printf ( "Error loading character set utf8: %s\n", $mysqli->error );
}

Related

Cannot reachable php-mysql connection from html code

I'm callling php function in html code. But connection between mysql and php is failed.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>go to main page</h1>
<?php actorList()?>
</body>
</html>
<?php
$conn = mysqli_connect("localhost", "root", "*","final");
if(!$conn) die("Connection failed!");
function actorList(){
global $conn;
$result = mysqli_query($conn, "SELECT * FROM actor_list");
}
?>
Why I cannot use $conn in actorList?
Why not pass into a param instead?
<?php
$conn = mysqli_connect("localhost", "root", "qotktk12","final");
if(!$conn) die("Connection failed!");
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>go to main page</h1>
<?php actorList($conn)?>
</body>
</html>
<?php
function actorList($conn){
$result = mysqli_query($conn, "SELECT * FROM actor_list");
}
?>
Or this way will work
<?php
$conn = mysqli_connect("localhost", "root", "","test");
if(!$conn) die("Connection failed!");
function actorList(){
global $conn;
$result = mysqli_query($conn, "SELECT * FROM actor_list");
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>go to main page</h1>
<?php actorList()?>
</body>
</html>

How can I add `charset` in php part?

I wrote following code for my page with utf-8 charset command:
<meta http-equiv="Content-Type" content="text/html; charset="utf-8">
<title> . . . </title>
<style type="text/css";>
<!--
.mystyle {
font-size:12px;
}
-->
</style>
<?php
$myDirectory = opendir(".");
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
closedir($myDirectory);
$indexCount = count($dirArray)-2;
print("<table border=0 cellpadding=3 cellspacing=0 class='mystyle'>\n");
print("<tr> . . . </tr>\n");
.
.
.
print("<td align=right>");
print(number_format(filesize($dirArray[$index])/1024));
}
.
.
.
print("<td><a href=\"$dirArray[$index]\">
.
.
.
}
print("</table>\n");
?>
When I run this code, it is run in correct mode, but with writing files in a list, it doesn't work properly in charset="utf-8" mode. How can I add charset in php part?
Setting the charset in php is made like this :
header("Content-Type: text/html; charset=UTF-8") ;
NB It should be sent before to write in the output, so, before your <meta http-equiv="Content-Type" content="text/html; charset="utf-8">.
<?php
header("Content-Type: text/html; charset=UTF-8") ;
?><!doctype html><html>
<meta http-equiv="Content-Type" content="text/html; charset="utf-8">
...

How to embedd a PDF file from Database / PHP in HTML

I want to get a PDF File from a BLOB in my MySQL Database. The PHP for showing the PDF works fine:
<?php
$mysqli = new mysqli("192.168.1.11", "root", "password", "DB");
if ($mysqli->connect_errno) {
die("Connection refused: " . $mysqli->connect_error);
}
try{
$sql = "SELECT file FROM table WHERE id = 1";
$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_object($result);
header('Content-type: application/pdf');
echo $row->file;
}catch(Exception $e){
echo "caught exception: ", $e->getMessage(), "\n";
}
?>
Now i want to embed this PDF File into a HTML. I tried something like this:
<?php
$pdf = "MY_PDF_FILE.PDF"; ◄
echo <<<BLOCK
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Embeded PDF</title>
</head>
<body>
<embed src="$pdf" type="application/pdf" height="25%" width="25%">
</body> ▲
</html>
BLOCK;
?>
I tried to save the $row->file; in the $pdf with $pdf = $row->file;. Always when i try to call the index.php of my website, it shows up, that the PDF File cannot be displayed and i only see the browsers PDF Viewer.
Can someone help me with this? :/
you can use an iframe element to show PDF.
You can load your base 64 encoded blob to data attribute of iframe.
Inside your data attribute you need to write: 'application/pdf;base64,' before base64 string.
<iframe data="data:application/pdf;base64,B64STRINGHERE" type="application/pdf" style="height:200px;width:60%"></iframe>
So you have to do something like:
<?php
// Connection to DB + Retrive blob
// We assume that your '$row->file' contains Blob data.
// So encode '$row->file' to base 64
$pdf = base64_encode($row->file); // BLOB TO BASE64
echo <<<BLOCK
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Embeded PDF</title>
</head>
<body>
<iframe data="data:application/pdf;base64,$pdf" type="application/pdf" style="height:200px;width:60%"></iframe>
</body>
</html>
BLOCK;
?>

PHP UTF8 not displaying chinese characters properly

I am trying to echo a chinese word in php from a table data but it seems it is not displaying properly
Here's my code
<?php
echo'<?xml version="1.0" encoding="utf-8"?>
<!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" xml:lang="zh-Hans" lang="zh-Hans">
';
?>
<head>
<title>A Test Page</title>
</head>
<body>
<?php
//Insert connection string
require_once 'confx/confx.php';
$ID = 222;
$conn = odbc_connect($odbc_dsn, $odbc_usr, $odbc_pwd);
if(!$conn) { die('Epic Fail!'); }
$query = odbc_exec($conn, "SELECT * FROM member WHERE userid = '$ID'");
$result = odbc_result($query, 'usernick');
echo $result;
odbc_free_result($query);
?>
</body>
</html>
I have saved the source code into UTF-8 and yet it is not working properly, Instead of displaying the supposed text, it prints out ???
Remove this line echo'<?xml version="1.0" encoding="utf-8"?>. Then, rework your code to look like this (HTML5 compliant). Keep the note of the meta attribute in header:
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<title>A Test Page</title>
<meta charset="utf-8">
</head>
<body>
<?php
// Start PHP code from here
$value = "黄后乎";
echo $value;
?>
</body>
</html>
Note: The HTML lang attribute can be used to declare the language of a Web page or a portion of a Web page. This is meant to assist search engines and browsers.
EDIT:
You have to specify the SQL`s result character encoding, by instructing the MySQL server, BEFORE your actual query, like this:
$query = odbc_exec($conn, "SET NAMES 'utf8'");
$query = odbc_exec($conn, "SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
//Your actual DB query
$query = odbc_exec($conn, "SELECT * FROM member WHERE userid = ". (int) $ID);
//Note the `(int) $ID` section, this is in order to prevent SQL injection attack.
<!DOCTYPE html>
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<html lang="zh-Hans">
<head>
<title>A Test Page</title>
<head>
<title>A Test Page</title>
</head>
<body>
<?php
$value = "黄后乎";
echo $value;
?>
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>A Test Page</title>
<head>
<title>A Test Page</title>
</head>
<body>
<?php
$value = "黄后乎";
echo $value;
?>

Can't view text as utf8 in php script

I have a script for real estate, it contains a locale file for translation, i translated it but the text displayed as question marks... tried the following with no success
adding
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
in head element
adding AddDefaultCharset utf-8 to .htaccess
adding AddDefaultCharset utf-8 to httpd.ini
adding this lines to php.ini
default_charset = "utf-8"
mbstring.internal_encoding=utf-8
mbstring.http_output=UTF-8
mbstring.encoding_translation=On
mbstring.func_overload=6
here are some script pages:
index.php (control panel, here the translated text is also garbled)
<?php
ob_start();
?>
<!doctype html>
<html>
<head>
<title>Property Listing</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
{APP_TPL}
</body>
</html>
<?php include 'listings.php'; ?>
Listing.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<?php
if (!isset($_GET['iframe']))
{
$content = ob_get_contents();
ob_end_clean();
ob_start();
}
if (!isset($_GET['controller']) || empty($_GET['controller']))
{
$_GET["controller"] = "Listings";
}
if (!isset($_GET['action']) || empty($_GET['action']))
{
$_GET["action"] = "index"
include dirname(__FILE__) . '/index.php';
if (!isset($_GET['iframe']))
{
$app = ob_get_contents();
ob_end_clean();
$app = str_replace('$','$',$app);
echo preg_replace('/\{APP_TPL\}/', $app, $content);
}
?>
preview (the main visitor page, all the text garbled)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<?php
ob_start();
?>
<!doctype html>
<html>
<head>
<title>Property Listing</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
{APP_TPL}
</body>
</html>
<?php include 'listings.php'; ?>
en.php (locale file)
#Login
$PL_LANG['login_login'] = 'Admin login';
$PL_LANG['login_username'] = "المستخدم";
$PL_LANG['login_password'] = "كلمة السر";
$PL_LANG['login_login'] = "Admin Login";
$PL_LANG['login_register'] = "تسجيل";
$PL_LANG['login_err'][1] = "Wrong username or password";
$PL_LANG['login_err'][2] = "Access denied";
$PL_LANG['login_err'][3] = "Account is disabled";
$PL_LANG['login_error'] = "Error";
# Left menu
$PL_LANG['menu_home'] = "Home";
$PL_LANG['menu_properties'] = "Properties";
$PL_LANG['menu_options'] = "Options";
$PL_LANG['menu_install'] = "Install";
$PL_LANG['menu_preview'] = "Preview";
$PL_LANG['menu_logout'] = "Logout";
$PL_LANG['menu_users'] = "Users";
# General
$PL_LANG['_yesno']['T'] = "Yes";
$PL_LANG['_yesno']['F'] = "No";
Make sure the editor you are using to save those files is writing the file as UTF-8.
You can also try converting the text to UTF-8
$utf8_text = mb_convert_encoding($non_utf8, 'UTF-8', mb_detect_encoding($non_utf8));

Categories