Get notifications from database - php

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.

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'

Related

Fetch all data in array

I am trying to add the direction, left and right member to direct but the problem now here is that I am only able to fetch one data (left_mem) instead of both left_mem and right_mem.
$query = $MySQLi_CON->query("select * from users where enroller_id='".$enroller_id_n."' ");
$direct = array();
if($query){
while ($row = $query->fetch_array()) {
$enroller_id3 = $row['enroller_id'];
$direct[] = $row['direction'];
}
}
if ($direct == "left_mem")
{
echo "success";
}
else {
echo "fail";
}
This is my database
CREATE TABLE `users` (
`user_id` int(11) NOT NULL,
`user_name` varchar(25) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_pass` varchar(255) NOT NULL,
`enroller_id` varchar(25) NOT NULL,
`enrolled_id` varchar(25) NOT NULL,
`direction` varchar(25) NOT NULL DEFAULT 'avail'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `users` (`user_id`, `user_name`, `user_email`, `user_pass`, `enroller_id`, `enrolled_id`, `direction`);
ALTER TABLE `users`
ADD UNIQUE KEY `user_id` (`user_id`);
ALTER TABLE `users`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
Use in_array to see if both values exist:
if (in_array('left_mem',$direct) && in_array('right_mem',$direct) )

select from 2 table and make ordering them

first, sorry for my English ...
what i want is to select from two SQL tables and then make them in a specific order , like in forums ...
i have two table, topic and users, i want to select from both of them a putt author info next to his topic
here is the SQL of Topic and users
CREATE TABLE IF NOT EXISTS `topics` (
`id` int(11) NOT NULL,
`id2` int(11) NOT NULL,
`title` varchar(256) NOT NULL,
`message` longtext NOT NULL,
`author_id` int(11) NOT NULL,
`timestamp` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `users` (
`id` bigint(20) NOT NULL,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`avatar` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
and the php code might look like this
<?php
$sql = mysql_query(' MySQL query ... ');
while($row = mysql_fetch_array($sql)) {
echo '<p>'.$row['username']'<br>';
echo $row['message'].'<br></p>';
}
?>
is there any way to do it ??
As I understood, what you are looking for is the correct SQL statement to execute. The following simple solution will.
<?php
$sql = mysql_query('SELECT users.username, topics.message FROM `users` INNER JOIN topics ON topics.author_id = users.id');
while($row = mysql_fetch_array($sql)) {
echo '<p>'.$row['username']'<br>';
echo $row['message'].'<br></p>';
}
?>
SELECT * FROM `users` INNER JOIN topics ON topics.author_id = users.id'

Query fetching all the posts of the users I am following and my own post

I have three tables
User > id, username, name, age, sex, location
User Post > id, user_id, description, image, postime, location
Follow > id, user_id, follow_id, status
In User table I will be having all the user information.
In User Post table I will be having all the post related information.
I want to display my own post and also post of the users I am following
The query I had written for this situation is
SELECT
u.username,
u.name,
u.profile_pic,
up.*
FROM user u, user_post up
WHERE (up.user_id = $user_id OR up.user_id IN
(SELECT user_id
FROM follow WHERE follow_id=$user_id)), $user_id)
AND description='' group by id order by postime desc
I feel the query is wrong, can anyone help me
CREATE TABLE IF NOT EXISTS `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`username` varchar(30) NOT NULL,
`password` varchar(1000) NOT NULL,
`email` varchar(100) NOT NULL,
`age` varchar(100) NOT NULL,
`gender` varchar(100) NOT NULL,
`city` varchar(100) NOT NULL,
`state` varchar(100) NOT NULL,
`country` varchar(100) NOT NULL,
`profile_pic` varchar(100) NOT NULL,
`deviceToken` char(64) DEFAULT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE IF NOT EXISTS `user_post` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`description` varchar(100) NOT NULL,
`image` varchar(100) NOT NULL,
`location` varchar(100) NOT NULL,
`postime` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` int(20) NOT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE IF NOT EXISTS `follow` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`user_id` int(100) NOT NULL,
`follow_id` int(100) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(20) NOT NULL DEFAULT 'Pending',
PRIMARY KEY (`id`)
)
This is the same answer I posted yesterday:
SELECT u.username,u.name,u.profile_pic, up.*
FROM user u JOIN user_post up
ON up.user_id = u.id
WHERE
(up.user_id = $user_id
OR
up.user_id IN (
SELECT user_id
FROM follow
WHERE follow_id=$user_id
)
)
AND description=''
ORDER BY postime DESC
As you didn't tell about the relations between user and follower it might enough to switch follow_idand user_id:
SELECT u.username,u.name,u.profile_pic, up.*
FROM user u JOIN user_post up
ON up.user_id = u.id
WHERE
(up.user_id = $user_id
OR
up.user_id IN (
SELECT follow_id
FROM follow
WHERE user_id=$user_id
)
)
AND description=''
ORDER BY postime DESC

Select two columns from two tables and order by their values DESC

With this code I'm getting news from the database and order it by column "views":
<?php
$getnewsinfo = mysql_query("SELECT * FROM news ORDER BY views DESC LIMIT 5");
while($newsinforow = mysql_fetch_array($getnewsinfo))
{
$newsid = $newsinforow['id'];
$title = $newsinforow['title'];
$author = $newsinforow['author'];
$date = date('d.m.Y', $newsinforow['date']);
$picture = $newsinforow['picture'];
$picture_desc = $newsinforow['picture_desc'];
$category = $newsinforow['category'];
$text = $newsinforow['text'];
?>
Now I want to order the news by views and by the number of comments, but I have no idea how.
Here is my database structure :
CREATE TABLE IF NOT EXISTS `news` (
`id` int(11) NOT NULL auto_increment,
`views` int(11) NOT NULL default '0',
`title` varchar(255) NOT NULL,
`author` varchar(255) NOT NULL,
`date` varchar(255) NOT NULL,
`picture` varchar(255) NOT NULL,
`picture_desc` varchar(255) NOT NULL,
`category` varchar(255) NOT NULL,
`text` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
CREATE TABLE IF NOT EXISTS `news_comments` (
`id` int(11) NOT NULL auto_increment,
`news_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`website` varchar(255) NOT NULL,
`text` text NOT NULL,
`date` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
I also want to ask you... do you have any comments on the code and database structure ?
Try this sql
SELECT n.*,count(nc.id) as cnt FROM news as n, news_comments as nc where n.id = nc.news_id group by nc.news_id ORDER BY views DESC,cnt DESC LIMIT 5

Recipe Finder with PHP and MySQL based on Ingredients

I am developing a cooking recipe-website and i want to create a recipe finder based on the used incredients.
My current finder only works with 3 ingredients right.
The Finder should return the right recipe(s) based on the used incredients (should work with 1-n*)
My Tables:
CREATE TABLE IF NOT EXISTS `INGREDIENTS` (
`ingredients_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`ingredients_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
CREATE TABLE IF NOT EXISTS `INGREDIENTS_POS` (
`ingredients_pos_id` int(11) NOT NULL AUTO_INCREMENT,
`ingredients_id` int(11) NOT NULL,
`ingredients_unit` varchar(20) NOT NULL,
PRIMARY KEY (`ingredients_pos_id`),
KEY `ingredients_detail_fk` (`ingredients_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
CREATE TABLE IF NOT EXISTS `RECIPES` (
`recipes_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) COLLATE utf8_bin NOT NULL,
`text` varchar(2000) COLLATE utf8_bin NOT NULL,
`count_persons` int(11) NOT NULL,
`duration` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`date` datetime NOT NULL,
`accepted` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`recipes_id`),
KEY `recipes_user_fk` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=88 ;
CREATE TABLE IF NOT EXISTS `RECIPES_POS` (
`recipes_pos_id` int(11) NOT NULL AUTO_INCREMENT,
`recipes_id` int(11) NOT NULL,
`ingredients_id` int(11) NOT NULL,
`ingredients_value` int(11) NOT NULL,
PRIMARY KEY (`recipes_pos_id`),
KEY `recipe_pos_rec_id` (`recipes_id`),
KEY `recipes_pos_ingredient_fk` (`ingredients_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=58 ;
My buggy Solution (doesn't support count from 1-n):
<?php
include 'db_connect.php';
$q = urldecode(mysql_real_escape_string($_GET['q']));
$parameter = explode ('$',$q);
$var = 0;
//print_r($parameter);
foreach($parameter as $ing)
{
//echo $ing;
$sql = "SELECT ingredients_id FROM INGREDIENTS WHERE name='".$ing."'";
$result = mysql_query($sql,$db) or exit('{"Data":null,"Message":null,"Code":500}');
$row = mysql_fetch_array($result);
$arr_id[$var] = $row['ingredients_id'];
$var++;
}
//print_r($arr_id);
$sql = "SELECT r.recipes_id FROM RECIPES r, RECIPES_POS rp WHERE r.recipes_id = rp.recipes_id ";
foreach($arr_id as $id)
{
$sql .= "AND rp.ingredients_id =".$id . " ";
}
//echo $sql;
$result = mysql_query($sql,$db) or exit('{"Data":null,"Message":null,"Code":500}');
mysql_close($db);
$rec;
while($row = mysql_fetch_array($result))
{
//echo "test";
$_GET['id'] = $row['recipes_id'];
$rec= include('get_recipe_byID.php');
}
//print_r(mysql_fetch_array($result));
if (count($arr_id) == 0)
{
echo '{"Data":null,"Message":null,"Code":404}';
die();
}
?>
I need a better solution for that chase.
Maybe SQL itself will help me to find the right recipes
thx
That query helped me a lot:
select r.recipes_id
from RECIPES r
inner join RECIPES_POS rp on r.recipes_id = rp.recipes_id
where rp.ingredients_id in (4, 6)
group by r.recipes_id
having count(distinct rp.ingredients_id) = 2

Categories