gpt4 book ai didi

PHP 无法从数据库中获取 UTF8

转载 作者:行者123 更新时间:2023-11-29 00:27:34 25 4
gpt4 key购买 nike

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

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

我在本地主机上没有问题但是在服务器上上传文件后我有问题

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 加载数据.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 �& ...

ØÙ�Ù�Ù� Ù�اÙ� Ø´Ù�اÙ� Ù� عÛ�د سعÛ�د Ù�طر Ù�بار Ú© باد

«Ø§Ù�Ù�Ù�Ù� صÙ�Ù� عÙ�Û� Ù�ØÙ�Ù�د Ù�&Osla ...

最佳答案

尝试在 <?php 之后添加这一行

header('Content-Type: text/plain; charset=utf-8');

关于PHP 无法从数据库中获取 UTF8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18252313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com