Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5980473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:48:20+00:00 2026-05-22T21:48:20+00:00

I have a function A which loads the data from db if the user

  • 0

I have a function A which loads the data from db if the user has liked the image. I have another function B which loads the count for the total number of likes for the image. Both these functions return response using JSON.

If I call them individually, everything works fine, but if I call function B in function A, I get no JSON response and nothing happens although firebug does show two JSON arrays being outputted.

What is wrong with the code?

Function A:

public function loadLikes() {
        //sql query

            try
            {


                $query = $this->_db->prepare($sql);
        $params = array(':imageid' => $imageid, ':author' => $author);
        $query->execute($params); 

                //calling function B
                $this->countLikes($imageid);

                if ($query->rowCount() > 0) {

                    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
                    if ($row['like'] == '1') {
                        $response = json_encode(array('like' => true));
                        echo $response;
                        return TRUE;
                    }
                    elseif ($row['like'] == '2')  {
                        $response = json_encode(array('unlike' => true));
                        echo $response;
                        return TRUE;
                    }
                    else {
                        $error = "Invalid";
                        $response = json_encode(array('like' => false, 'text' => $error));
                        echo $response;
                        return FALSE;
                    }
                  }

                }
                else {

                    $response = json_encode(array('unlike' => true));
                    echo $response;
                    return FALSE;
                }

            }
            catch(PDOException $ex)
            {
                echo json_encode(array('like' => false, 'text' => $ex));
                return FALSE;
            }

    }

Function B:

public function countLikes($i) {
        //sql query

            try
            {           
                $query = $this->_db->prepare($sql);
                $params = array(':imageid' => $i);
                $query->execute($params); 

                if ($query->rowCount() > 0) {
                    $count = $query->fetchColumn();
                    $response = json_encode(array('count' => $count));
                    echo $response;
                    return TRUE;
                }


            }
            catch(PDOException $ex)
            {

                return FALSE;
            }

    }

jQuery:

$.ajax({
                            type: "POST",
                            url: url,
                            data: postData, 
                            dataType: "json",
                            success: function(data){
                                $(".count-like").show(600).text(data.count);
                                if(data.like) {
                                    $("a#alike").attr('class', 'starred');  
                                }
                                else if (data.unlike) {
                                    $("a#alike").attr('class', 'unlike');
                                }
                                else {
                                    alert(data.text);
                                }
                            }
                        });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:48:21+00:00Added an answer on May 22, 2026 at 9:48 pm

    If you invoke both functions, then each will output a JSON array. This will result in a HTTP response with following content:

    {"like":1}{"count":2}
    

    Both arrays would be valid separately. But if concatenated like this, it’s no longer valid JSON.

    Instead of outputting the json serialization with echo you should collect it in a temporary variable, merge the two arrays, and then output the combined array with a single:

    echo json_encode(array_merge($countArray, $likeArray));
    

    Example adaptions of your code

    Function B should become:

    public function countLikes($i) {
        //sql query
    
            try
            {           
                $query = $this->_db->prepare($sql);
                $params = array(':imageid' => $i);
                $query->execute($params); 
    
                if ($query->rowCount() > 0) {
                    $count = $query->fetchColumn();
    
                    /* JUST RETURN HERE */
                    return (array('count' => $count));
                }
    
    
            }
            catch(PDOException $ex)
            {
    
                /* INSTEAD OF FALSE use an empty array,
                   which is interpreted as false in boolean context*/
                return array();
            }
    
    }
    

    Then when you call the function:

    //calling function B
    $countArray = $this->countLikes($imageid);
    

    This will always be an array. Which you then can use in the output code:

    $response = json_encode(array('like' => true) + $countArray);
    

    (It’s still inadvisable to have an echo right there. But too much code, too little context to propose a nicer rewrite. And if it works, ..)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which gets a key from the user and generates a
I have the following javascript code, which loads without error, however the update function
I have an function which decodes the encoded base64 data in binary data but
i have a function which retrieves values from a webservice , then loops through
Say I have a C function which takes a variable number of arguments: How
I have a function which parses one string into two strings. In C# I
I have a function which searches an STL container then returns the iterator when
Imagine I have an function which goes through one million/billion strings and checks smth
This is a Windows Forms application. I have a function which captures some mouse
i have a c function which returns a long double . i'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.