کلیدستان

نسخه‌ی کامل: گرفتن خروجی json از دیتا بیس بر روی سرور با نمایش حرف فارسی utf8
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
فقط کافیه این کدو در یک فایل php ذخیره کنید و بر روی سرور اپلود کنید
کانفینگ سرور رو هم انجام دهید
تمامی فیلدهای جدول خروجی میده
و مشکل خواندن اطلاعات فارسی در صفحه حل شده
کد پی‌اچ‌پی:
<?php
header
("Content-type: text/html; charset=utf8");
    
$mysql mysql_connect('localhost','user','pass');
    
mysql_select_db('dbname');
mysql_query("SET NAMES UTF8");
$query 'select * from tabelename';
$res mysql_query($query);

    
// iterate over every row
    
while ($row mysql_fetch_assoc($res)) {
        
// for every field in the result..
        
for ($i=0$i mysql_num_fields($res); $i++) {
            
$info mysql_fetch_field($res$i);
            
$type $info->type;
 
            
// cast for real
            
if ($type == 'real')
                
$row[$info->name] = doubleval($row[$info->name]);
            
// cast for int
            
if ($type == 'int')
                
$row[$info->name] = intval($row[$info->name]);
        }

        
$rows[] = $row;
    }
    
$toast json_encode($rows);
$toast preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function($matches) {return mb_convert_encoding(pack('H*'$matches[1]), 'UTF-8''UTF-16');}, $toast);
echo 
'{ "Goo":',"$toast","}"