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;
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:
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:
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 ?
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));
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.
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 :
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.
get_sender and get_partner:
You are getting the sender and partner ids using the news id from above function.
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.