Still having problems passing variables

I use the following function to fetch news items:

    function get_news_by_sender() {
        
        $sql = "SELECT * 
                  FROM news 
                 WHERE sender_id = ?
                   AND partner_id = ? 
                 ORDER 
                    BY add_date DESC";

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(array($this->sender_id,$this->partner_id));

        $results = $stmt->fetchAll();

        return $results;

Front-end I have the following:

$news_items = $cNews->get_news_by_sender();

<?php foreach ($news_items as $news) : ?>
   <p>NEWS ID: <?php echo $news['news_id'] ?> - <?php echo $news['news_content'] ?></p>
<?php endforeach; ?>

But for some reason I don’t get the list with messages. When I changes the values in the array to hard coded values:

$stmt->execute(array(1,2));

I get the list with messages. Now you probably think that the variables $this->sender_id; and $this->partner_id; are not properly set but they are. To test it I have the two functions producing those values as well included front-end:

 <input id="sender_id" name="sender_id" type="text" value="<?php echo $cNews->get_sender();?>">
<input id="partner_id" name="partner_id" type="text" value="<?php echo $cNews->get_partner();?>">  

and they are giving me the right values.

What am I doing wrong?

Thank you in advance

Trying typecast :

$stmt->execute(array((int) $this->sender_id, (int) $this->partner_id));

I tried that but still no change?

Can we see $cNews->get_sender() function definition ?

Yes ofcource

    function get_sender(){ 
        $sql = "SELECT sender_id FROM news WHERE news_id = ?";

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(array((int)$this->news_id));

        $row = $stmt->fetch();
        $this->sender_id = $row['sender_id'];

        return $this->sender_id;
    }

Here you are :slight_smile:

So there must be something wrong with the order of calling the function and property assignment. So either you have to call the function get_sender() before execute or use directly $this->get_sender(). Try something like this:

$stmt->execute(array($this->get_sender(),$this->get_partner()));

Or call those functions before execute:

$this->get_sender();
$this->get_partner();
$stmt->execute(array($this->sender_id,$this->partner_id));

I tried both but still no results

Strange ! Can you post the whole class then ? I am not sure about passing the values to the views but it should work in the model and controllers. Did you try var_dump or print_r(results) in the model itself ?

Have you tried checking $stmt->errorInfo ?

In get_news_by_sender, you use $this->sender_id, but in your test, you use $this->get_sender(). So I’m afraid this test doesn’t really confirm anything.

Here’s what you should do instead:

echo "The value of \$this->sender_id is {$this->sender_id}\n";
echo "The value of \$this->partner_id is {$this->partner_id}\n";
$stmt->execute(array($this->sender_id,$this->partner_id));
3 Likes

Hi Jeff,
I just tried that and the echo’s didn’t give me any results. So what is wrong in your opinion?

Thank you in advance.

Just to clarify, does no result mean you got “The value of $this->sender_id is” and then no value? Or do you mean you got nothing whatsoever? Not even the first part of the sentence?

I’m hoping it’s the former, otherwise there are even bigger issues going on.

If it is indeed the former, then the problem is simply that sender_id and partner_id are in fact never set, and you’ll have to make sure they are before you call get_news_by_sender.

Hi Jeff.

Luckily It’s indeed the former

What do I need to show to see If I missed out on something?

Is there a spot in your script where you expected sender_id to get set?

Jeff, please check the following function that he has posted already above:

function get_sender(){ 
        $sql = "SELECT sender_id FROM news WHERE news_id = ?";
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(array((int)$this->news_id));
        $row = $stmt->fetch();
        $this->sender_id = $row['sender_id'];
        return $this->sender_id;
    }

So it is supposed to be set right in there. Now I suspect that his query in this function not selecting any data it seems. That’s why I was asking him to show the whole class including all the codes and how he has passed the values to the views.

Hi Raju. This is the complete test class incl Jeff’s test part

class test {
    
    protected $pdo;
    public $news_id;
    public $sender_id;
    public $partner_id;
    
    function __construct($pdo) {
        $this->pdo = $pdo;
    }
    
    function get_news(){
        $sql = "SELECT MIN(news_id) AS news_id
                  FROM news
                 WHERE isNew = 1";
        
        $stmt = $this->pdo->query($sql);
        $row = $stmt->fetch();
        $this->news_id = $row['news_id'];

        return $this->news_id;    
    }
    
    function get_sender(){ 
        $sql = "SELECT sender_id FROM news WHERE news_id = ?";

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(array((int)$this->news_id));

        $row = $stmt->fetch();
        $this->sender_id = $row['sender_id'];

        return $this->sender_id;
    }
    
    function get_partner(){ 
        $sql = "SELECT partner_id FROM news WHERE news_id = ?";

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute(array($this->news_id));

        $row = $stmt->fetch();
        $this->partner_id = $row['partner_id'];

        return $this->partner_id;
    }    

    function get_news_by_sender() {
        
        $sql = "SELECT * 
                  FROM news 
                 WHERE sender_id = ?
                   AND partner_id = ? 
                 ORDER 
                    BY add_date DESC";

        $stmt = $this->pdo->prepare($sql);
        $this->get_sender();
        $this->get_partner();
        echo "The value of \$this->sender_id is {$this->sender_id}\n\n";
        echo "The value of \$this->partner_id is {$this->partner_id}\n\n";
        $stmt->execute(array($this->sender_id,$this->partner_id));

        $results = $stmt->fetchAll();
        
        return $results; 
    }                
}
Hope that helps

Hmm it is kinda weird to know what exactly you are trying to achieve. Let me summarize what you are doing :

  1. ge t_news:
  • You are trying to get all the news flagged as isNew=1.
  • Then fetched the first row from the result set and set the news_id from it.
  1. get_sender and get_partner:
  • You are getting the sender and partner ids using the news id from above function.
  1. get_news_by_sender:
  • Now you are trying to get all the news using the same sender_id and partner_id from the same news.

70% of the code above is unncessary I am sure if it is the actual code. If it is an example even that does not sound good.

Well to solve your problem now, you can just call the function get_news right after the prepare statement and it will work if there are records in the database (news table).

$sql = "SELECT * 
                  FROM news 
                 WHERE sender_id = ?
                   AND partner_id = ? 
                 ORDER 
                    BY add_date DESC";
        $stmt = $this->pdo->prepare($sql);
        $this->get_news();
        $this->get_sender();
        $this->get_partner();
        $stmt->execute(array($this->sender_id,$this->partner_id));
        $results = $stmt->fetchAll();

I am sure this should work now but think about what you are trying to achieve from the above code.

Hi Raju. That is working indeed. Thank you very much, it’s very much appreciate.

With this code I get all messages where the sender_id and the partner_id the same are that is all.