json_encode(mysql数据库|表charset utf8)

时间:2016-04-15 07:01:58

标签: php json utf-8 character-encoding

在我的代码json_encode($result)中没有工作,但如果我print_r($result)它将输出。现在我知道这只是一个字符集编码问题,我知道在php中将输出转换为UTF8的许多不同方法,但我的问题更多地与我的输出不是UTF8时为什么我的数据库和表都是UTF8时开始?

function conn(){
    $dsn = 'mysql:host=localhost;dbname=classicmodels';
    $username = '*******';
    $password = '*******';
    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 
    $db = new PDO($dsn, $username, $password, $options);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    return $db;
 }

$db = conn();
$stmt = $db->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($result) //outputs fine
//echo json_encode($result);

print_r($result)输出如下:

Array
(
    [0] => Array
        (
            [customerNumber] => 103
            [customerName] => Atelier graphique
        )

    [1] => Array
        (
            [customerNumber] => 112
            [customerName] => Signal Gift Stores
        )

    [2] => Array
        (
            [customerNumber] => 114
            [customerName] => Australian Collectors, Co.
        )

    [3] => Array
        (
            [customerNumber] => 119
            [customerName] => La Rochelle Gifts
        )

    [4] => Array
        (
            [customerNumber] => 121
            [customerName] => Baane Mini Imports
        )

    [5] => Array
        (
            [customerNumber] => 124
            [customerName] => Mini Gifts Distributors Ltd.
        )

    [6] => Array
        (
            [customerNumber] => 125
            [customerName] => Havel & Zbyszek Co
        )

    [7] => Array
        (
            [customerNumber] => 128
            [customerName] => Blauer See Auto, Co.
        )

    [8] => Array
        (
            [customerNumber] => 129
            [customerName] => Mini Wheels Co.
        )

    [9] => Array
        (
            [customerNumber] => 131
            [customerName] => Land of Toys Inc.
        )

    [10] => Array
        (
            [customerNumber] => 141
            [customerName] => Euro+ Shopping Channel
        )

    [11] => Array
        (
            [customerNumber] => 144
            [customerName] => Volvo Model Replicas, Co
        )

    [12] => Array
        (
            [customerNumber] => 145
            [customerName] => Danish Wholesale Imports
        )

    [13] => Array
        (
            [customerNumber] => 146
            [customerName] => Saveley & Henriot, Co.
        )

    [14] => Array
        (
            [customerNumber] => 148
            [customerName] => Dragon Souveniers, Ltd.
        )

    [15] => Array
        (
            [customerNumber] => 151
            [customerName] => Muscle Machine Inc
        )

    [16] => Array
        (
            [customerNumber] => 157
            [customerName] => Diecast Classics Inc.
        )

    [17] => Array
        (
            [customerNumber] => 161
            [customerName] => Technics Stores Inc.
        )

    [18] => Array
        (
            [customerNumber] => 166
            [customerName] => Handji Gifts& Co
        )

    [19] => Array
        (
            [customerNumber] => 167
            [customerName] => Herkku Gifts
        )

    [20] => Array
        (
            [customerNumber] => 168
            [customerName] => American Souvenirs Inc
        )

    [21] => Array
        (
            [customerNumber] => 169
            [customerName] => Porto Imports Co.
        )

    [22] => Array
        (
            [customerNumber] => 171
            [customerName] => Daedalus Designs Imports
        )

    [23] => Array
        (
            [customerNumber] => 172
            [customerName] => La Corne D'abondance, Co.
        )

    [24] => Array
        (
            [customerNumber] => 173
            [customerName] => Cambridge Collectables Co.
        )

    [25] => Array
        (
            [customerNumber] => 175
            [customerName] => Gift Depot Inc.
        )

    [26] => Array
        (
            [customerNumber] => 177
            [customerName] => Osaka Souveniers Co.
        )

    [27] => Array
        (
            [customerNumber] => 181
            [customerName] => Vitachrome Inc.
        )

    [28] => Array
        (
            [customerNumber] => 186
            [customerName] => Toys of Finland, Co.
        )

    [29] => Array
        (
            [customerNumber] => 187
            [customerName] => AV Stores, Co.
        )

    [30] => Array
        (
            [customerNumber] => 189
            [customerName] => Clover Collections, Co.
        )

    [31] => Array
        (
            [customerNumber] => 198
            [customerName] => Auto-Moto Classics Inc.
        )

    [32] => Array
        (
            [customerNumber] => 201
            [customerName] => UK Collectables, Ltd.
        )

    [33] => Array
        (
            [customerNumber] => 202
            [customerName] => Canadian Gift Exchange Network
        )

    [34] => Array
        (
            [customerNumber] => 204
            [customerName] => Online Mini Collectables
        )

    [35] => Array
        (
            [customerNumber] => 205
            [customerName] => Toys4GrownUps.com
        )

    [36] => Array
        (
            [customerNumber] => 206
            [customerName] => Asian Shopping Network, Co
        )

    [37] => Array
        (
            [customerNumber] => 209
            [customerName] => Mini Caravy
        )

    [38] => Array
        (
            [customerNumber] => 211
            [customerName] => King Kong Collectables, Co.
        )

    [39] => Array
        (
            [customerNumber] => 216
            [customerName] => Enaco Distributors
        )

    [40] => Array
        (
            [customerNumber] => 219
            [customerName] => Boards & Toys Co.
        )

    [41] => Array
        (
            [customerNumber] => 223
            [customerName] => Nat�rlich Autos
        )

    [42] => Array
        (
            [customerNumber] => 227
            [customerName] => Heintze Collectables
        )

    [43] => Array
        (
            [customerNumber] => 233
            [customerName] => Qu�bec Home Shopping Network
        )

    [44] => Array
        (
            [customerNumber] => 237
            [customerName] => ANG Resellers
        )

    [45] => Array
        (
            [customerNumber] => 239
            [customerName] => Collectable Mini Designs Co.
        )

    [46] => Array
        (
            [customerNumber] => 240
            [customerName] => giftsbymail.co.uk
        )

    [47] => Array
        (
            [customerNumber] => 242
            [customerName] => Alpha Cognac
        )

    [48] => Array
        (
            [customerNumber] => 247
            [customerName] => Messner Shopping Network
        )

    [49] => Array
        (
            [customerNumber] => 249
            [customerName] => Amica Models & Co.
        )

    [50] => Array
        (
            [customerNumber] => 250
            [customerName] => Lyon Souveniers
        )

    [51] => Array
        (
            [customerNumber] => 256
            [customerName] => Auto Associ�s & Cie.
        )

    [52] => Array
        (
            [customerNumber] => 259
            [customerName] => Toms Spezialit�ten, Ltd
        )

    [53] => Array
        (
            [customerNumber] => 260
            [customerName] => Royal Canadian Collectables, Ltd.
        )

    [54] => Array
        (
            [customerNumber] => 273
            [customerName] => Franken Gifts, Co
        )

    [55] => Array
        (
            [customerNumber] => 276
            [customerName] => Anna's Decorations, Ltd
        )

    [56] => Array
        (
            [customerNumber] => 278
            [customerName] => Rovelli Gifts
        )

    [57] => Array
        (
            [customerNumber] => 282
            [customerName] => Souveniers And Things Co.
        )

    [58] => Array
        (
            [customerNumber] => 286
            [customerName] => Marta's Replicas Co.
        )

    [59] => Array
        (
            [customerNumber] => 293
            [customerName] => BG&E Collectables
        )

    [60] => Array
        (
            [customerNumber] => 298
            [customerName] => Vida Sport, Ltd
        )

    [61] => Array
        (
            [customerNumber] => 299
            [customerName] => Norway Gifts By Mail, Co.
        )

    [62] => Array
        (
            [customerNumber] => 303
            [customerName] => Schuyler Imports
        )

    [63] => Array
        (
            [customerNumber] => 307
            [customerName] => Der Hund Imports
        )

    [64] => Array
        (
            [customerNumber] => 311
            [customerName] => Oulu Toy Supplies, Inc.
        )

    [65] => Array
        (
            [customerNumber] => 314
            [customerName] => Petit Auto
        )

    [66] => Array
        (
            [customerNumber] => 319
            [customerName] => Mini Classics
        )

    [67] => Array
        (
            [customerNumber] => 320
            [customerName] => Mini Creations Ltd.
        )

    [68] => Array
        (
            [customerNumber] => 321
            [customerName] => Corporate Gift Ideas Co.
        )

    [69] => Array
        (
            [customerNumber] => 323
            [customerName] => Down Under Souveniers, Inc
        )

    [70] => Array
        (
            [customerNumber] => 324
            [customerName] => Stylish Desk Decors, Co.
        )

    [71] => Array
        (
            [customerNumber] => 328
            [customerName] => Tekni Collectables Inc.
        )

    [72] => Array
        (
            [customerNumber] => 333
            [customerName] => Australian Gift Network, Co
        )

    [73] => Array
        (
            [customerNumber] => 334
            [customerName] => Suominen Souveniers
        )

    [74] => Array
        (
            [customerNumber] => 335
            [customerName] => Cramer Spezialit�ten, Ltd
        )

    [75] => Array
        (
            [customerNumber] => 339
            [customerName] => Classic Gift Ideas, Inc
        )

    [76] => Array
        (
            [customerNumber] => 344
            [customerName] => CAF Imports
        )

    [77] => Array
        (
            [customerNumber] => 347
            [customerName] => Men 'R' US Retailers, Ltd.
        )

    [78] => Array
        (
            [customerNumber] => 348
            [customerName] => Asian Treasures, Inc.
        )

    [79] => Array
        (
            [customerNumber] => 350
            [customerName] => Marseille Mini Autos
        )

    [80] => Array
        (
            [customerNumber] => 353
            [customerName] => Reims Collectables
        )

    [81] => Array
        (
            [customerNumber] => 356
            [customerName] => SAR Distributors, Co
        )

    [82] => Array
        (
            [customerNumber] => 357
            [customerName] => GiftsForHim.com
        )

    [83] => Array
        (
            [customerNumber] => 361
            [customerName] => Kommission Auto
        )

    [84] => Array
        (
            [customerNumber] => 362
            [customerName] => Gifts4AllAges.com
        )

    [85] => Array
        (
            [customerNumber] => 363
            [customerName] => Online Diecast Creations Co.
        )

    [86] => Array
        (
            [customerNumber] => 369
            [customerName] => Lisboa Souveniers, Inc
        )

    [87] => Array
        (
            [customerNumber] => 376
            [customerName] => Precious Collectables
        )

    [88] => Array
        (
            [customerNumber] => 379
            [customerName] => Collectables For Less Inc.
        )

    [89] => Array
        (
            [customerNumber] => 381
            [customerName] => Royale Belge
        )

    [90] => Array
        (
            [customerNumber] => 382
            [customerName] => Salzburg Collectables
        )

    [91] => Array
        (
            [customerNumber] => 385
            [customerName] => Cruz & Sons Co.
        )

    [92] => Array
        (
            [customerNumber] => 386
            [customerName] => L'ordine Souveniers
        )

    [93] => Array
        (
            [customerNumber] => 398
            [customerName] => Tokyo Collectables, Ltd
        )

    [94] => Array
        (
            [customerNumber] => 406
            [customerName] => Auto Canal+ Petit
        )

    [95] => Array
        (
            [customerNumber] => 409
            [customerName] => Stuttgart Collectable Exchange
        )

    [96] => Array
        (
            [customerNumber] => 412
            [customerName] => Extreme Desk Decorations, Ltd
        )

    [97] => Array
        (
            [customerNumber] => 415
            [customerName] => Bavarian Collectables Imports, Co.
        )

    [98] => Array
        (
            [customerNumber] => 424
            [customerName] => Classic Legends Inc.
        )

    [99] => Array
        (
            [customerNumber] => 443
            [customerName] => Feuer Online Stores, Inc
        )

    [100] => Array
        (
            [customerNumber] => 447
            [customerName] => Gift Ideas Corp.
        )

    [101] => Array
        (
            [customerNumber] => 448
            [customerName] => Scandinavian Gift Ideas
        )

    [102] => Array
        (
            [customerNumber] => 450
            [customerName] => The Sharp Gifts Warehouse
        )

    [103] => Array
        (
            [customerNumber] => 452
            [customerName] => Mini Auto Werke
        )

    [104] => Array
        (
            [customerNumber] => 455
            [customerName] => Super Scale Inc.
        )

    [105] => Array
        (
            [customerNumber] => 456
            [customerName] => Microscale Inc.
        )

    [106] => Array
        (
            [customerNumber] => 458
            [customerName] => Corrida Auto Replicas, Ltd
        )

    [107] => Array
        (
            [customerNumber] => 459
            [customerName] => Warburg Exchange
        )

    [108] => Array
        (
            [customerNumber] => 462
            [customerName] => FunGiftIdeas.com
        )

    [109] => Array
        (
            [customerNumber] => 465
            [customerName] => Anton Designs, Ltd.
        )

    [110] => Array
        (
            [customerNumber] => 471
            [customerName] => Australian Collectables, Ltd
        )

    [111] => Array
        (
            [customerNumber] => 473
            [customerName] => Frau da Collezione
        )

    [112] => Array
        (
            [customerNumber] => 475
            [customerName] => West Coast Collectables Co.
        )

    [113] => Array
        (
            [customerNumber] => 477
            [customerName] => Mit Vergn�gen & Co.
        )

    [114] => Array
        (
            [customerNumber] => 480
            [customerName] => Kremlin Collectables, Co.
        )

    [115] => Array
        (
            [customerNumber] => 481
            [customerName] => Raanan Stores, Inc
        )

    [116] => Array
        (
            [customerNumber] => 484
            [customerName] => Iberia Gift Imports, Corp.
        )

    [117] => Array
        (
            [customerNumber] => 486
            [customerName] => Motor Mint Distributors Inc.
        )

    [118] => Array
        (
            [customerNumber] => 487
            [customerName] => Signal Collectibles Ltd.
        )

    [119] => Array
        (
            [customerNumber] => 489
            [customerName] => Double Decker Gift Stores, Ltd
        )

    [120] => Array
        (
            [customerNumber] => 495
            [customerName] => Diecast Collectables
        )

    [121] => Array
        (
            [customerNumber] => 496
            [customerName] => Kelly's Gift Shop
        )

)

问题:如果我的db / table都是UTF8,为什么我在php中的数据库输出还不是UTF8格式?

1 个答案:

答案 0 :(得分:-1)

print_r(json_encode($result));会奏效。 echo(json_encode($result));将无效。由于它是json数组,因此无法使用 echo

打印数组