无法从PHP上的MySQL响应中检索信息

时间:2019-05-14 11:28:33

标签: php mysql

我能够从MySQL服务器获得响应,但是我似乎无法将其放在变量中

$filmNameList2 = [];
require('connect.php');

$query = "SELECT `title`,`year` FROM `filmList` WHERE year=' (2019)'";

$result = mysqli_query($connect, $query) or die(mysqli_error($connect));

$json_array = array();
    while($row=mysqli_fetch_array($result))
{
        $json_array[] = $row;
// print_r($row); outputs Array ( [0] => Abruptio [title] => Abruptio [1] => (2019) [year] => (2019) )

    }
$filmNameList2[] = $json_array->array[0]->array[0]->title;
// I have tried json_array->array[0]->title; json_array->title;
print_r($filmNameList2);

我得到的结果:

Array ( [0] => )

3 个答案:

答案 0 :(得分:0)

$filmNameList2 = array();
require('connect.php');

$query = "SELECT `title`,`year` FROM `filmList` WHERE year=' (2019)'";

$result = mysqli_query($connect, $query) or die(mysqli_error($connect));

    while($row=mysqli_fetch_array($result))
{
       $object = array(
        'title'   => $row[0],
        'year'    => $row[1]
    );

    array_push($filmNameList2 , $object );
    }
echo json_encode($filmNameList2);
print_r($filmNameList2);

答案 1 :(得分:0)

尝试此代码...

 $query = "SELECT `title`,`year` FROM `filmList` WHERE year=' (2019)'";

 $result = mysqli_query($connect, $query) or die(mysqli_error($connect));

 $json_array = array();
  while($row=mysqli_fetch_object($result))
  {
    $json_array[] = $row;
  }
   echo json_encode($json_array); 
  print_r($json_array);

答案 2 :(得分:0)

$filmNameList2[] = $json_array[0]['title'];

访问第一行,然后访问其中的title元素。