Jump to content

[SOLVED] MySQL and PHP News


Navarr

Recommended Posts

So, I'm pulling news from a MySQL database.  I have it setup so that if the language is different, the news changes (JP or en-US).  The problem is that the UTF-8 Japanese characters in the MySQL database are not pulling through PHP correctly, and all PHP is outputting is question marks.  The page is setup to display in UTF-8 and is working as such (because the other japanese characters in the language files are working properly) but the ones pulled from MySQL do not.  How can I fix this within PHP?  I know its possible, because phpMyAdmin shows the Japanese characters.  Can someone please please please please Help?  The Code for the pages effected are below.

 

index.php

<?php
        require('inc.php');
        $tpl->assign('page','news');
        $browser = (array)get_browser();
        $tpl->assign('browser',$browser['browser']);
        $forum = 1;
        if ($language == 'en-US') { $forum = 1; }
        if ($language == 'JP') { $forum = 2; }
        $news = array();
        $q = mysql_query("SELECT * FROM `xt_bbs_posts` WHERE `parent` IS NULL AND `forum`=".$forum." ORDER BY `time` DESC LIMIT 0,10") or die(mysql_error());
        while($r = mysql_fetch_array($q)) {
                $i = count($news);
                $news[$i]['ico'] = '';
                $news[$i]['icoalt'] = '';
                $news[$i]['title'] = $r['title'];
                $news[$i]['time'] = lang_time($language,$r['time']);
                $news[$i]['date'] = lang_date($language,$r['time']);
                $news[$i]['post'] = $r['post'];
                $news[$i]['poster'] = $r['uid'];
                $news[$i]['commamt'] = 0;
        }
        $tpl->assign('news',$news);
        $tpl->display('index.tpl');
?>

 

index.tpl

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg-flat.dtd">
<html xmlns             = "http://www.w3.org/1999/xhtml" xml:lang="en-US"
      xmlns:svg         = "http://www.w3.org/2000/svg"
      xmlns:xlink       = "http://www.w3.org/1999/xlink">
<head>
  <title>{$lang.title}{$title}</title>
  <link rel="stylesheet" type="text/css" href="/style.css" />
</head>
<body>{strip}
  <div id="logodiv"><h1 style="visibility:hidden;">POKéQUEST - Pokémon Online Game - Pokemon Online Game - Pokemon MMO - Pokémon MMO - Pokemon MMORPG - Pokémon MMORPG</h1></div>
  <div id="navidiv">
   {include file="navi.tpl"}
  </div>
  <div id="maindiv">
   {if $browser == "IE"}
   <div id="iewarning">THIS PAGE DOES NOT WORK PROPERLY IN IE, PLEASE GET <a href="http://opera.com/">OPERA</a> OR <a href="http://getfirefox.com">FIREFOX</a></div>
   {/if}
   {include file="$page.tpl"}
  </div>
{/strip}</body>
</html>

 

news.tpl

{strip}{section name=i loop=$news}
<table class="news_table">
  <tr>
   <th class="news_table_topic"><img src="{$news[i].ico}" alt="{$news[i].icoalt}" title="{$news[i].icoalt}" /> {$news[i].title}</th>
   <td class="news_table_time">{$news[i].date}<br />{$news[i].time}</td>
  </tr>
  <tr>
   <td class="news_table_post" colspan="2">{$news[i].post}</td>
  </tr>
  <tr>
   <td class="news_table_poster"><a href="{$news[i].posterurl}">{$news[i].poster}</a></td>
   <td class="news_table_comment"><a href="{$news[i].commurl}">{$news[i].commamt} {if $news[i].commamt == 0}Comments{/if}{if $news[i].commamt > 1}Comments{/if}{if $news[i].commamt == 1}Comment{/if}</a></td>
  </tr>
</table>
<br />
{/section}{/strip}

Link to comment
https://forums.phpfreaks.com/topic/48202-solved-mysql-and-php-news/
Share on other sites

With a billion more Google Queries, and finally finding something that works.  I am so very pleased that this is no longer an issue (no thanks to you people :P)  This is how to get it to work, if you ever exprience a similar problem:

 

Just for everyone's information, if you want to run purely utf8, don't forget the following:

 

httpd.conf:

AddCharset UTF-8 .utf8

AddDefaultCharset UTF-8

 

php.ini

default_charset = "utf-8"

 

my.cnf

character-set-server=utf8

default-collation=utf8_unicode_ci

 

Then, if you're writing PHP scripts, soon after opening your connection to mysql, issue one of the following:

SET NAMES 'utf8';

OR, if you are running the mysqli extension:

mysqli_set_charset('utf8');

 

Finally, in your HTML, don't forget the following in your head section: charset=utf-8

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.