Jump to content

Why am I getting am empty string return value from method call?


Go to solution Solved by drezzia,

Recommended Posts

I have two methods in a class. The first is 

save_comment()

It basically just inserts comment into db and then after its successfully another method is called to load some comments and the method is named 

load_comments()

 

This is the code for better understanding. Unnecessary part had been commented out. 

 

 function save_comment()
    {
        //$this->Functions->debug();

        //request token
        //$token = $this->Functions->clean($_GET["token"]);

        //check for csrf
       // $check = $this->Functions->csrf_check($token);

        /*if ($check === false) {
            $this->Functions->api_response("Invalid Request!!!", 0, "csrf");
        }*/

        $pid = $this->Functions->clean($_GET["pid"]);

        $offset = $this->Functions->clean("", $_GET["offset"], "");

        //comment
        $comment = $this->Functions->clean($_GET["user_comment"]);

        //check login status
       /* if (!isset($_SESSION["user"])) {
            $this->Functions->api_response(
                "You must login to make comments !!!.",
                0,
                "csrf"
            );
        }*/

        //check if comment is less than 5 charcaters.
       /* if (strlen($comment) < 5) {
            $this->Functions->api_response(
                "Your Comment is too short !!!.",
                0,
                "csrf"
            );
        }*/

        try {
            $user = $_SESSION["user"];

            $params = ["sss", "?,?,?", [$pid, $user, $comment]];

            $save_comment = $this->CustomDB->insert(
                "comments",
                "pid,user,comment",
                $params
            );


            if ($save_comment->affected_rows == 1) {
                $offset = 0;

                $data = $this->load_comments($pid, $offset);
         
echo $data;//this returns empty string

                $this->Functions->api_response(
                    "Thanks!! Your Comment has been submitted 🤗 ",
                    1,
                    "csrf",
                    $data
                );
            } else {
                throw new Exception("Something went wrong. Try again!!!");
            }
        } catch (Exception $e) {
            //try

            $msg = $e->getMessage();

            $this->Functions->api_response($msg, 0, "csrf");
        }
    } //comment()

Now the second method is

 function load_comments($pid, $offset)
    {
        $this->Functions->debug();

        //get comments so we can update at frontend
        $params = ["si", [$pid, $offset]];

        //fetch comments
        $fetch = $this->CustomDB->query_custom(
            "SELECT * FROM comments WHERE pid=? ORDER BY id DESC LIMIT 10 OFFSET?",
            $params
        );

        $fetch = $fetch["result"];


        $html = "";

        //$html;

        while ($row = $fetch->fetch_assoc()) {
            $name = $row["user"];

            $comment = $row["comment"];
            $html .=
                '<div class="columns is-mobile display"><div style="border-radius:30px 30px 30px 30px;" class="column is-full box"><p class="subtitle is-6 mb-3"><em>' .
                $comment .
                '</em></p><span class="bold ml-2">' .
                $name .
                "</span></div></div><br>";
                
             
        }

        return $html;

       
    }

I can't really figure why it shouldn't work as expected

  • Solution

Thanks for your help...

After spending several hours, I was able to figure where the error lies. All I posted was actually OK but the method for querying db that is inside load_comments() had lil issues. The line `$this->query_custom()` This is how the query_custom() method looks like...     

function query_custom($sql, $params = "")
    {

        $stmt = $this->con->prepare($sql);

        if (isset($params)) {
            $stmt->bind_param($params[0], ...$params[1]);
        }

        $stmt->execute();

        $res = $stmt->get_result();

        $row = $res->fetch_assoc();
        
        $count = $res->fetch_row();
        
        return [
            "result" => $res,
            "execute" => $stmt,
            "row" => $row,
            "count" => $count,
        ];
    }
    

Since I want to loop through the results with a while loop then I should have left out this line...  `$row = $res->fetch_assoc()` and therefore return *$res*. So when I changed it to what I have below, it works as expected.

 

function query_custom($sql, $params = "")
    {
       
        $stmt = $this->con->prepare($sql);

        if (isset($params)) {
            $stmt->bind_param($params[0], ...$params[1]);
        }

        $stmt->execute();

        $res = $stmt->get_result();
        
        return [
            "result" => $res,
            "execute" => $stmt,
            
        ];
    }
    

 

 

Edited by drezzia
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.