PHP无法从数据库中获取UTF8

时间:2013-08-15 12:07:45

标签: php jquery mysql

我正在使用ajax分页方法搜索结果。设置mysql项目连接并设置utf8之后,我的结果是'utf8 unicode,

我的dabtabase数据是utf8并且正确保存。但是在获取后我无法获得正确的unicode。

我在localhost上没有问题,但在服务器上传文件后我有问题

JQUERY:

$(document).ready(function(){
    $('#searchWordButton').click(function(){
        searchWord = $('#searchWord').val();
        if ( $.trim(searchWord) =='' ){
            alert('input is empty...');
            return false;
        } 
        else{
        $('[id^="hidden_div"]').hide();
            $('.div_search').show();                
            function loadData(page){
                $.ajax
                ({
                    type: "POST",
                    url: "load_data.php",
                    data: "page="+page+"&word="+searchWord,
                    success: function(msg)
                    {
                        $("#search_container").html(msg);
                    }
                });
            }
            loadData(1);  // For first time page load default results
            $('#search_container').on('click','.pagination li.active',function(){
                var page = $(this).attr('p');
                loadData(page);
            });                             
        }

    });

PHP load_data.php:

<?php
if($_POST['page'])
{
$page = $_POST['page'];
$word = $_POST['word'];
$cur_page = $page;
$page -= 1;
$per_page = 22;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;

include 'config.inc';

$linkConnection = mysql_connect (LOCALHOST , USERNAME , DBpass);
mysql_query("set charset set utf8", $linkConnection);
mysql_query("set names 'utf8'", $linkConnection);           
mysql_select_db(DBname);

$query_pag_data = "SELECT id as msg_id ,subject as message , description from contents WHERE subject like '%$word%' LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());


$msg = "";
while ($row = mysql_fetch_array($result_pag_data)) {
    $subject     = htmlentities(htmlspecialchars_decode($row['message']));
    $description = htmlentities(htmlspecialchars_decode($row['description']));

    $msg .= "<div style='font-weight:bold;color:#063B27;'><a href='viewPage.php?topicID={$row['msg_id']}'>".$subject . "</a></div>";
    $msg .= "<div style='color:#378686;padding-right:30px;'>".mb_substr($description,0,200,'UTF-8').' ...' . "</div>";
}
$msg = "<div class='data'>" . $msg . "</div>"; // Content for Data

$query_pag_num = "SELECT COUNT(*) AS count FROM contents WHERE subject like '%$word%' ";
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);

if ($cur_page >= 7) {
    $start_loop = $cur_page - 3;
    if ($no_of_paginations > $cur_page + 3)
        $end_loop = $cur_page + 3;
    else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
        $start_loop = $no_of_paginations - 6;
        $end_loop = $no_of_paginations;
    } else {
        $end_loop = $no_of_paginations;
    }
} else {
    $start_loop = 1;
    if ($no_of_paginations > 7)
        $end_loop = 7;
    else
        $end_loop = $no_of_paginations;
}

$msg .= "<div class='pagination'><ul>";

// FOR ENABLING THE FIRST BUTTON
if ($first_btn && $cur_page > 1) {
    $msg .= "<li p='1' class='active'>ابتدا</li>";
} else if ($first_btn) {
    $msg .= "<li p='1' class='inactive'>ابتدا</li>";
}

// FOR ENABLING THE PREVIOUS BUTTON
if ($previous_btn && $cur_page > 1) {
    $pre = $cur_page - 1;
    $msg .= "<li p='$pre' class='active'>صفحه قبل</li>";
} else if ($previous_btn) {
    $msg .= "<li class='inactive'>صفحه قبل</li>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {

    if ($cur_page == $i)
        $msg .= "<li p='$i' style='color:#fff;background-color:#006699;' class='active'>{$i}</li>";
    else
        $msg .= "<li p='$i' class='active'>{$i}</li>";
}

// TO ENABLE THE NEXT BUTTON
if ($next_btn && $cur_page < $no_of_paginations) {
    $nex = $cur_page + 1;
    $msg .= "<li p='$nex' class='active'>صفحه بعد</li>";
} else if ($next_btn) {
    $msg .= "<li class='inactive'>صفحه بعد</li>";
}

// TO ENABLE THE END BUTTON
if ($last_btn && $cur_page < $no_of_paginations) {
    $msg .= "<li p='$no_of_paginations' class='active'>انتها</li>";
} else if ($last_btn) {
    $msg .= "<li p='$no_of_paginations' class='inactive'>انتها</li>";
}
$total_string = "<span class='total' a='$no_of_paginations'>صفحه <b>" . $cur_page . "</b> از <b>$no_of_paginations</b></span>";
$msg = $msg . "</ul>"  . $total_string . "</div>";  // Content for pagination

echo $msg;
    }

结果:

Ø¹Û Ø¯Ø³Ø¹Û Ø¯Ù Ø·Ø±Ø¯Ø±Ø¨ØØØØز

ر٠زد٠Ø'٠ب30Ø'Ù Ø±Û Ù Ø±1388Ù &amp; ...

Ø٠٠٠٠٠٠ا٠Ø'Ù Ø§Ù Ù Ø¹Û Ø¯Ø³Ø¹Û Ø¯Ù Ø·Ø±Ù Ø¨Ø§Ø± کباد

Øا٠٠٠٠ØμÙ Ù Ø¹Ù Û Ù Û Ø٠٠د٠&amp; Osla ......

1 个答案:

答案 0 :(得分:1)

尝试在<?php

之后添加此行
header('Content-Type: text/plain; charset=utf-8');