mysql - FullCalendar, JSON array empty but working on PHP file -


i configuring fullcalendar mysql db, using php process , return json.

  • db-connect.php - fetches results db , encodes json.
  • get-events.php - reads json, converts fullcalendar
  • json.html - front-end calendar view

file contents below, before reading: db-connect.php outputs json have verified on jsonlint.

[{"title":"test new calendar","start":"2015-07-21","end":"2015-07-22"}] 

get-events.php 'reading' db-connect.php "php/get-events.php must running." error message on front-end view has disappeared (shows if example can't establish db-connect.php in directory, or spelling error in file name, etc).

however when either pass query via params or check in firebug console, json array empty.

/cal/demos/php/get-events.php?start=2015-07-01&end=2015-07-31 

returns [] whereas test calendar entry fall within these parameters.

i'm convinced it's db-connect.php error, i'm scratching head it. relative newbie i'm sure it's obvious!

db-connect.php

<?php     $db = mysql_connect("localhost:3306","root","");     if (!$db) {         die('could not connect db: ' . mysql_error());     }      mysql_select_db("test",$db);      $result = mysql_query("select * cal", $db);        $json_response = array();      while ($row = mysql_fetch_array($result, mysql_assoc)) {         $row_array['id'] = $row['id'];         $row_array['title'] = $row['title'];         $row_array['start'] = $row['start'];         $row_array['end'] = $row['end'];          array_push($json_response,$row_array);     }     echo json_encode($json_response);      mysql_close($db);  ?> 

get-events.php

<?php    // require our event class , datetime utilities require dirname(__file__) . '/utils.php';   if (!isset($_get['start']) || !isset($_get['end'])) {     die("please provide date range."); }   $range_start = parsedatetime($_get['start']); $range_end = parsedatetime($_get['end']);   $timezone = null; if (isset($_get['timezone'])) {     $timezone = new datetimezone($_get['timezone']); }  $json = file_get_contents(dirname(__file__) . '/db-connect.php'); $input_arrays = json_decode($json, true);  $output_arrays = array();  if (is_array($input_arrays) || is_object($input_arrays)) { foreach ($input_arrays $array) {      $event = new event($array, $timezone);      if ($event->iswithindayrange($range_start, $range_end)) {         $output_arrays[] = $event->toarray();     } } }  echo json_encode($output_arrays); 

file_get_contentsdoesn't parse php file. output programmcode in case. add function get-events.php

function loadphpfile($file) {     ob_start();      include $file;     $content = ob_get_contents();     ob_end_clean();     return $content; } 

and replace

$json = file_get_contents(dirname(__file__) . '/db-connect.php'); 

with

$json = loadphpfile(dirname(__file__) . '/db-connect.php'); 

and hint: please use objects (oop) , mysqli. php mysqli


Comments