2
\$\begingroup\$

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;
}
\$\endgroup\$
1
\$\begingroup\$

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;
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ thank u sir but .. my issue got solved using above method \$\endgroup\$ – user2604422 Mar 5 '14 at 3:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.