\$\begingroup\$
\$\endgroup\$
0
Call from my index.php to function get_data($curr_date)
goes from here
case "events" :
$curr_date = $_POST['current_date'];
if ( $eid = get_data($curr_date) )
{
$result = array( "success"=>true,
"eid"=>$eid);
$json_output = json_encode( $result );
echo $json_output;
}
else
echo FAIL;
break;
enter code here
This is my function which returns the result
function get_data($curr_date)
{
$events_list = mysql_query( "SELECT eid, display_date, word_of_the_day, word_of_the_day_meaning, thought_of_the_day,
crazyFact_of_the_day, joke_of_the_day FROM eventsoftheday WHERE display_date = '$curr_date' ");
// get mysql result cursor
if( mysql_num_rows( $events_list ) > 0 ) // check if the query returned any records
{
$mysql_record = mysql_fetch_array( $events_list ); // parse mysql result cursor
$events = array();
$events['e_id'] = $mysql_record['eid'];
$events['edisplay_date'] = $mysql_record['display_date'];
$events['eword_of_the_day'] = $mysql_record['word_of_the_day'];
$events['eword_of_the_day_meaning'] = $mysql_record['word_of_the_day_meaning'];
$events['ethought_of_the_day'] = $mysql_record['thought_of_the_day'];
$events['ecrazyFact_of_the_day'] = $mysql_record['crazyFact_of_the_day'];
$events['ejoke_of_the_day'] = $mysql_record['joke_of_the_day'];
return $events; // return events
}
else
return false;
}
asked Feb 28, 2014 at 7:07
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
Maybe it's better to return error in JSON (if FAIL constant isn't JSON yet)
echo json_encode(array('result' => false));
instead of
echo FAIL;
or return HTTP error code
header('HTTP/1.1 500 Internal Server Error');
And we can simplifify function get_data like this:
if(mysql_num_rows( $events_list )) // check if the query returned any records
{
$mysql_record = mysql_fetch_array( $events_list ); // parse mysql result cursor
return array(
'e_id' => $mysql_record['eid'],
'edisplay_date' => $mysql_record['display_date'],
'eword_of_the_day' => $mysql_record['word_of_the_day'],
'eword_of_the_day_meaning' => $mysql_record['word_of_the_day_meaning'],
'ethought_of_the_day' => $mysql_record['thought_of_the_day'],
'ecrazyFact_of_the_day' => $mysql_record['crazyFact_of_the_day'],
'ejoke_of_the_day' => $mysql_record['joke_of_the_day'],
);
} else return false;
answered Feb 28, 2014 at 9:40
-
\$\begingroup\$ thank u sir but .. my issue got solved using above method \$\endgroup\$user2604422– user26044222014年03月05日 03:35:18 +00:00Commented Mar 5, 2014 at 3:35
lang-php