0

I'm able to get the response from MySQL server, but I can't seem to put it in a variable

$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);

result that I get :

Array ( [0] => )
Vikrant
  • 4,920
  • 17
  • 48
  • 72
  • 1
    What if you simply `print_r($row)` within your while loop. Do you actually get any results showing then? And if so, could you add that response to your question? – Dirk Scholten May 14 '19 at 11:31
  • Possible duplicate of [How can I access an array/object?](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) – FMK May 14 '19 at 11:31
  • I think there is space in `where year=' (2019)'` should be `where year='(2019)'` – Rahul Singh May 14 '19 at 11:39
  • Firstly you should try run your query (`SELECT title.year FROM filmList WHERE year=' (2019)'`) directly in MySQL (using phpMyAdmin or something similar) and check if it even returns any results. Your query looks suspicious. – Volvox May 15 '19 at 05:52

3 Answers3

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);
Raahul
  • 399
  • 1
  • 3
  • 11
0

try this code...

 $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);
0

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

Access the first row, then the title element in it.

Joffrey Schmitz
  • 2,393
  • 3
  • 19
  • 28