我怎样才能在php中打印对象值

时间:2017-09-25 09:47:10

标签: php mysql

这是我的PHP代码:

    <?php 
    //Creating a connection
    $con = mysqli_connect("****","****","****","****");
    $con->set_charset("utf8");

    if (mysqli_connect_errno())
    {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $strcode = $_GET["id"];

    $sql = "Select movie_image from map_table2 where movie_image = '".$strcode."' ORDER BY id DESC LIMIT 1 ";

    $result = mysqli_query($con ,$sql);

    while ($row = mysqli_fetch_assoc($result)) {

        $array[] = $row;

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

    echo json_encode($array, JSON_UNESCAPED_UNICODE );

    mysqli_free_result($result);

    mysqli_close($con);

?>

当我运行php时,我得到了这些数据:

  

[{&#34; movie_image&#34;:&#34; 231320166&#34;}]

但我想得到这些数据:

  

231320166

我应该使用什么代码?

2 个答案:

答案 0 :(得分:1)

取代:

json_encode($array, JSON_UNESCAPED_UNICODE );

使用:

echo $array[0]['movie_image'];

答案 1 :(得分:1)

使用json_decode() http://php.net/manual/en/function.json-decode.php

<?php

$json = '[{"movie_image":"231320166"}]';
$array = json_decode($json, true);
$image = $array[0]['movie_image'];
echo $image;

在此处查看https://3v4l.org/lrnL6