【问题标题】:PHP : how to accommodate the data containing the symbols of the database into an array json?PHP:如何将包含数据库符号的数据容纳到数组 json 中?
【发布时间】:2016-11-02 14:29:46
【问题描述】:

我想使用 json 显示来自 wo_wks_desc 列的数据,但是有一些数据不可见,因为 cel 的内容包含字符符号 Ø、Ã 等。

帮我解决这个问题。

我的代码就是这样并显示 json 结果,我在 list_barcode2.php 中编写代码

    <?php 
include "./class/connect_sql.class.php";
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) )
{


    
    $query = mssql_query("select  ROW_NUMBER() OVER(ORDER BY barcode_wo_nmr) nomor, barcode_wo_nmr, barcode_wo_line,wo_wks_desc,
			barcode_wo_proses, barcode_npk, barcode_start, barcode_pending, barcode_end,barcode_qty,barcode_ng,barcode_reason_pending
			from WO_ENG_BARCODE, WO_ENG_DET, WO_ENG_DET_WKS  with(nolock) where 
			barcode_wo_nmr= WO_DET_NOMOR and barcode_line = wo_det_line  and barcode_wo_nmr = wo_wks_nmr and 
			WO_DET_NOMOR = wo_wks_nmr and barcode_wo_line = wo_wks_line and WO_DET_NAMA_BARANG = wo_wks_barang and right(barcode_wo_nmr,2) >= '15'
			order by barcode_wo_nmr desc");
    $jsonResult = '{"data" : [ ';
    $i=0;
    while ($data=mssql_fetch_assoc($query)) 
	{
       if($i != 0)
	   {
           $jsonResult .=',';
       }
	   
       $jsonResult .=json_encode($data);
	   
	   $i++;
	 
   }
   $jsonResult .= ']}';
	
    echo $jsonResult;
} 
else {
    echo '<script>window.location="404.html"</script>';
}
?>

    jQuery(document).ready(function() {
		var table = jQuery('#dataTables').DataTable( {
                    "ajax": "./pc_list_barcode.php",
                    "order": [[ 1, 'asc' ]],
                    "columns": [
                        { "data": "nomor" },
                        { "data": "barcode_wo_nmr" },
						{ "data": "barcode_wo_line" },
						{ "data": "wo_wks_desc" },
                       
						
						{ "data": "barcode_wo_proses" },
						{ "data": "barcode_npk" },
						{ "data": "barcode_start",
						 "width": "120px"
						},
						{ "data": "barcode_pending" },
						{ "data": "barcode_end" },
						{ "data": "barcode_qty" },
						{ "data": "barcode_ng" },
						{ "data": "barcode_reason_pending",
							"width": "120px"
							},
						{
							"data": "barcode_wo_nmr",
							"render": function(data){
								return '<font color="blue"><a href="edit_barcode.php?id=' + data + '">EDIT</a>' 
							}
						}	
						//{"data": "barcode_reason_pending"},
						// {"data": return '<a href="edit_barcode.php?id=' + row.id + '">EDIT</a>' },
                    ]
                } );

【问题讨论】:

  • 不要这样尝试,请尝试 json_encode(array("data"=>$complete_data)),其中 $complete_data 是包含所有获取记录的数组
  • 是这样的吗? while json_encode($data=mssql_fetch_assoc($query))
  • 不,将所有记录放在一个数组中,然后对该数组进行json_encode。

标签: php json ajax datatables symbols


【解决方案1】:

使用这个:

$rows = array();
while ($data = mysql_fetch_assoc($query)) {
    $rows[] = $data;
}

$jsonResult = json_encode(array(
    "data" => $rows
));

为了正确编码所有符号,您需要在执行查询之前告诉 mysql,您希望数据以 UTF-8 编码,如下所示:

mysql_set_charset('utf8')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多