user3442879
user3442879

Reputation: 53

Get notifications from database

im making an notification system, and i can't get them outputted.

I have following code:

public function getNotification() {

$sth = $this->dbh->prepare("SELECT n.to_uid as `notif_to_uid`, n.status as `notif_status`, n.time as `notif_time`, u.id as `user_id`, u.name as `user_name` FROM notifications n INNER JOIN users u ON u.id = n.to_uid WHERE u.id = :uid n.type = 'friendrequest'");
$sth->execute(array(
    ':uid' => $_SESSION['uid']
)
);

if($notifications = $sth->fetchAll(PDO::FETCH_OBJ)) {
foreach($notifications as $notification) {

echo 'New notification ect. ect.';
}
}

}

My tables looks like this:

CREATE TABLE `notifications` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `from_uid` int(11) NOT NULL,
  `to_uid` int(11) NOT NULL,
  `type` enum('friendrequest','gameinvite','update') NOT NULL,
  `status` enum('unseen','seen') NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
);
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL,
  `password` varchar(155) NOT NULL,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL,
  `gender` varchar(255) NOT NULL,
  `points` varchar(255) DEFAULT '0',
  PRIMARY KEY (`id`)
);

Right now it isnt outputting anything, and the connection to the db does work.

Upvotes: 0

Views: 84

Answers (1)

turson
turson

Reputation: 421

Try to display query errors by

$execute = $sth->execute(array(
    ':uid' => $_SESSION['uid']
)
);
if(!$execute){
    print_r($sth->errorInfo());
}

I see you have lost "AND" or "OR" here

WHERE u.id = :uid n.type = 'friendrequest'

Upvotes: 1

Related Questions