Hello I got a website up and running. I added a forum to my website, and i need it to be able to input and output chinese characters, I googled around and added this mysql_query("SET NAMES 'utf8'"); before insert query. Now its able to insert chinese into the database, but all output turns into question mark. I think i need to add mysql_query("SET CHARACTER_SET_RESULTS=utf8"); or something, into the code. But could someone please tell me where do I put it in code below. thanks in advance.
// Includes
//========================================================================================================
if($HTTP_HOST == 'localhost' || $HTTP_HOST == '127.0.0.1' || ereg('^192\.168\.0\.[0-9]+$', $HTTP_HOST)) {
include('config_local.inc.php');
}
else {
include('config_main.inc.php');
}
if(!isset($language)) $language = 'en';
include("languages/lang_$language.inc");
include('smilies.inc');
include('funclib.inc');
//========================================================================================================
// Set session variables (admin login and message ID); needs PHP 4.1.0 or higher
//========================================================================================================
if($admin && $admin == $adminPass) $_SESSION['sf_admin'] = $admin;
if(!$new && $enableIDs && !$_SESSION['msgID']) {
srand((double) microtime() * 1000000);
$_SESSION['msgID'] = md5(uniqid(rand()));
}
//========================================================================================================
// Functions
//========================================================================================================
function showTreeItem($item, $level, $hilight) {
global $message, $wordLength, $forumWidth, $adminPass,
$forum, $tNr, $mNr, $lines, $open,
$tbl_name, $fld_id, $fld_date, $fld_subject, $fld_name, $fld_email;
$id = $item['id'];
$pid = $item['pid'];
$thread = $item['thread'];
$sql = "SELECT $fld_subject, $fld_date, $fld_name, $fld_email FROM $tbl_name WHERE $fld_id='$id'";
$row = mysql_fetch_row(mysql_query($sql));
$subject = format($row[0], $wordLength, $forumWidth - 105, true);
$date = $row[1];
$name = format($row[2], $wordLength, $forumWidth - 105, true);
$email = preg_match('/^[a-z0-9\.\_\-]+@[a-z0-9äöüÄÖÜ\.\-]+\.[a-z]{2,4}$/i', $row[3]) ? $row[3] : '';
if(($item['open'] || $tNr) && $item['replies']) {
$img = 'minus.gif';
$link = ($open || $mNr) ? '' : "$forum&showMessage=$pid";
}
else if($item['replies']) {
$img = 'plus.gif';
$link = ($open || $mNr) ? '' : "$forum&showMessage=$id";
}
else {
$img = 'point.gif';
$link = '';
}
?>
<table border="0" cellspacing="0" cellpadding="0"><tr valign="top">
<? for($i = 0; $i < $level; $i++): ?><td width="20"><img src="<? echo $lines[$i]; ?>" width="20" height="17"></td><? endfor; ?>
<td width="20"><img src="line.gif" width="20" height="3"><? if($link): ?><a href="<? echo $link; ?>"><? endif; ?><img src="<? echo $img; ?>" border="0" width="20" height="9"><? if($link): ?></a><? endif; ?><img src="<? echo $lines[$i]; ?>" width="20" height="5"></td>
<td nowrap>
<?
if($id == $hilight) echo '<span class="cssCurThread">';
else echo '<a href="' . "$forum&mNr=$id&tNr=$thread" . '" class="cssLink1">';
echo $subject;
echo ($id == $hilight) ? '</span>' : '</a>';
?>
<span class="cssDetails"><i>(<? echo $date; ?><? if($name): ?>, <? if($email): ?><a href="mailto:<? echo $email; ?>" class="cssLink2"><? endif; ?><? echo $name; ?></a><? endif; ?>)</i></span>
<? if($item['replies']): ?><span class="cssDate">[<? echo $item['replies']; ?>] [<? echo $item['update']; ?>]</span><? endif; ?>
<? if($_SESSION['sf_admin'] && $_SESSION['sf_admin'] == $adminPass): ?><a href="javascript:confirmDelete(<? echo "$id, " . (($mNr == $id) ? $pid : $mNr) . ", $thread"; ?>)"><img src="delete.gif" border="0" width="10" height="10" alt="<? echo $message[8]; ?>"></a><? endif; ?>
</td>
</tr></table>
<?
if(($item['open'] || $tNr) && $item['replies']) {
$cnt = 0;
while(list($key, $sub_item) = each($item['items'])) {
$last = (++$cnt >= count($item['items']));
$lines[$level+1] = $last ? 'blank.gif' : 'line.gif';
showTreeItem($sub_item, $level+1, $hilight);
}
}
}
function showThreads($tree, $hilight) {
global $threadsPerPage, $mNr, $tNr, $lines;
$total = count($tree);
$cnt = 0;
while(list($thread, $items) = each($tree)) {
$cnt++;
$last = ($tNr || $cnt == $total);
$lines[0] = $last ? 'blank.gif' : 'line.gif';
while(list($key, $item) = each($items)) {
showTreeItem($item, 0, $hilight);
}
}
}
function updateTreeItem(&$item, $date, $sub = false) {
global $open;
$item['replies']++;
if($date > $item['date']) $item['update'] = $date;
if($open || $sub) $item['open'] = true;
}
function addTreeItem(&$tree, $item) {
$id = $item['id'];
$pid = $item['pid'];
if($pid && $tree[$pid]) {
$tree[$pid]['items'][$id] = $item;
updateTreeItem($tree[$pid], $item['date'], $item['open']);
return true;
}
reset($tree);
while(list($key, $val) = each($tree)) {
if(addTreeItem($tree[$key]['items'], $item)) {
updateTreeItem($tree[$key], $item['date'], $item['open']);
return true;
}
}
return false;
}
function buildThreads($open = 0) {
global $threadsPerPage, $start, $date_show, $date_show_from, $tNr, $total_threads,
$tbl_name, $fld_id, $fld_date, $fld_thread, $fld_pid;
$errors = $cond = '';
$threads = $keys = array();
if($tNr) {
$cond = "$fld_thread='$tNr'";
}
else if($date_show_from && $date_show) {
$cond = "$fld_date>='$date_show_from' AND $fld_date<='$date_show'";
}
$sql = "SELECT $fld_thread, MAX($fld_id) FROM $tbl_name";
if($cond) $sql .= " WHERE $cond";
$sql .= " GROUP BY $fld_thread";
$result = mysql_query($sql);
if($result) while($row = mysql_fetch_row($result)) {
$threads[$row[0]] = $tNr ? array() : $row[1];
}
else sql_error();
$total_threads = count($threads);
if(!$tNr) {
arsort($threads);
$cnt = 0;
while(list($thread, $val) = each($threads)) {
if($cnt < $start || $cnt >= $start + $threadsPerPage) {
unset($threads[$thread]);
}
else {
$threads[$thread] = array();
$keys[] = $thread;
}
$cnt++;
}
if(count($keys)) $cond = "$fld_thread IN (" . join(',', $keys) . ")";
}
$sql = "SELECT $fld_id, $fld_pid, $fld_thread, $fld_date FROM $tbl_name";
if($cond) $sql .= " WHERE $cond";
$sql .= " ORDER BY $fld_id";
$result = mysql_query($sql);
if($result) while($row = mysql_fetch_row($result)) {
$item = array(
'id' => $row[0],
'pid' => $row[1],
'thread' => $row[2],
'date' => $row[3],
'open' => ($open == $row[0]) ? true : false,
'replies' => 0,
'update' => $row[3],
'items' => array()
);
if(!addTreeItem($threads[$row[2]], $item)) {
if(!$row[1]) $threads[$row[2]][$row[0]] = $item;
else $errors .= 'ERROR: could not add ID ' . $item['id'] . ' to tree<br>';
}
}
else sql_error();
if($errors) echo '<div class="cssError">' . $errors . '</div>';
return $threads;
}
//========================================================================================================
// Main
//========================================================================================================
if($open == '') $open = $openThreads ? 1 : 0;
$forum = $forum_all = "$PHP_SELF?open=$open";
$forum_date = "$forum&date_show";
if($start >= 0) $forum .= "&start=$start";
if($date_show) $forum .= "&date_show=$date_show";
$forum_open = preg_replace('/open=\d+/', 'open=1', $forum);
$forum_close = preg_replace('/open=\d+/', 'open=0', $forum);
header('Cache-control: private, no-cache, must-revalidate');
header('Expires: Sat, 01 Jan 2000 00:00:00 GMT');
header('Date: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Pragma: no-cache');
?>
<?
header("Content-Type: text/html; charset=utf-8");
?>