MySql, php: Read from 2 Tables with same User ID - php

Hello Sorry for the previously Post:
I habe a table ´Nachrichten´ and a table 'user'.
I want the rows from 'Nachrichten' where Quelle is like $_SESSION["id"]. Quelle is a UserID. Now I need the rows from 'user' where ID is like $_SESSION["id"], too beacause I need the name and lastname from the user: This is my Query but I get no faild but no results too. Thanks for support.
$abfrage = "SELECT * FROM Nachrichten n JOIN user u ON u.id = n.Quelle WHERE n.Quelle LIKE '".$_SESSION["id"]."'";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis))
{
$id = $row["n"]->ID; $ziel = $row["n"]->Ziel; $uhrzeit = $row["n"]->Uhrzeit; $gelesen = $row["n"]->Gelesen; $inhalt = $row["n"]->Inhalt; $vorname = $row["u"]->Vorname; $nachname = $row["u"]->Nachname;
kontakt($ziel, $vorname, $nachname, $uhrzeit, $gelesen);
echo "hjfe";
}

Just guessing...
$abfrage = "
SELECT *
FROM Nachrichten n
JOIN user u
ON u.id = n.Quelle
WHERE n.Quelle = '{$_SESSION['id']}';
";

Related

Complicated array comparison

1- I'm building a comparison app which compares the relationship between two users.
If the user A and user B has the same country address then it will print alert to any of the same similar values.
2- There is another issue which is some users has multiple addresses for example user A has address x1 and user b has address x1 and address x2, in this scenario how do I compare user A's address with the multiple addresses with user B and alert that the address x1 is matched between the two users?
here is the queries for both users
$query1 = "
SELECT
a.PID, a.`Name`, a.Avatar, a.Email,
b.City, a.LDate, b.Country, b.MPhone,
b.Gender, b.Birthday
FROM
`Profile` AS a
LEFT JOIN Details AS b ON a.PID = b.PID
WHERE
a.PID = '1'
";
$UserInfo1 = mysqli_query($mysqli, $query1);
while( $row = mysqli_fetch_array($UserInfo1) ){
$Avatar1 = "{$row['Avatar']}";
$Name1 = "{$row['Name']}";
$Email1 = "{$row['Email']}";
$Country1 = "{$row['Country']}</td>";
$MPhone1 = "{$row['MPhone']}</td>";
$Gender1 = "{$row['Gender']}</td>";
$City1 = "{$row['City']}</td>";
$Birthday1 = "{$row['Birthday']}</td>";
}
$query2 = "
SELECT
a.PID, a.`Name`, a.Avatar, a.Email,
b.City, a.LDate, b.Country, b.MPhone,
b.Gender, b.Birthday
FROM
`Profile` AS a
LEFT JOIN Details AS b ON a.PID = b.PID
WHERE
a.PID = '2'
";
$UserInfo2 = mysqli_query($mysqli, $query2);
while( $row = mysqli_fetch_array($UserInfo2) ){
$Avatar2 = "{$row['Avatar']}";
$Name2 = "{$row['Name']}";
$Email2 = "{$row['Email']}";
$Country2 = "{$row['Country']}</td>";
$MPhone2 = "{$row['MPhone']}</td>";
$Gender2 = "{$row['Gender']}</td>";
$City2 = "{$row['City']}</td>";
$Birthday2 = "{$row['Birthday']}</td>";
}
3- The final results is that I want to make flowchart and point the matched information to each other something like this screenshot:
What would be the best and simplest way to make this comparison in php or Mysql?
Your help is highly needed and appreciated.

PHP / mysql join

I need to get 3 tables's values , from first I need to get aff_id where v_id = 5 , from second I need to get user id where aff_id = first's aff_id , and from third I need to get username , email , id where id = second's aff_id . I can't write correct mysql query to get data , please , help me to get it . Here is my wrong code
SELECT * FROM wp_vendor_affiliates WHERE vendor_id = 21 LEFT JOIN wp_affiliate_wp_affiliates
SELECT wp_vendor_affiliates.affiliate_id ,
wp_affiliate_wp_affiliates.user_id
FROM wp_vendor_affiliates
INNER JOIN wp_affiliate_wp_affiliates INNER JOIN
Please , hetp me , and correct my query . Thanks fot helping and for support
Use this code
$id1 = 5;
$query1 = "SELECT * FROM table1 WHERE v_id='{$id1}'";
$result1 = mysqli_query($con,$query1);
$row1 = mysqli_fetch_array($result1);
$id2 = $row1['aff_id'];
$query2 = "SELECT * FROM table2 WHERE aff_id='{$id2}'";
$result2 = mysqli_query($con,$query2);
$row2 = mysqli_fetch_array($result2);
$id3 = $row2['aff_id'];
$query3 = "SELECT * FROM table3 WHERE id='{$id3}'";
$result3 = mysqli_query($con,$query3);
$row3 = mysqli_fetch_array($result3);
$id2 = $row3['aff_id'];
Here $con is the connection to your database
$con= mysqli_connect("localhost","root","","data_base");
I used here connection to the localhost. which you'd have to change most certainly. It's a bit long but basic

PHP MYSQL Nested While loop skips iterations

Need help to get through this Nested WHILE loop.
The Scenario is
These are the 5 tables in my db
user_info(userid,name,picurl)
user_friends(userid,friend_id)
user_post(userid,post_id,post,time)
user_likes(userid,post_id)
user_comments(userid,post_id)
I want to access user_post table and populate the data in my android application. i can access userid,post_id and time from user_post table and using friend_id of the user from friends table. Now to display a complete post with pic and name i need to access name and picurl of the person who's post it is from user_info table. If i need to display one 1 friends post i can do this simply but when there are couple of friends with more than 1 post i can't get the name and picurl of the user and it displays null in json response.
I'm using Nested While loop for this operation following is my code.
$userid = $_POST['userid'];
$query = "SELECT * FROM user_friend WHERE userid = '$userid'";
$result = mysql_query($query);
$return_arr = array();
$return_arr['newsfeed'] = array();
//Access userid
while($row = mysql_fetch_assoc($result)) {
echo "Friend Name is ".$row['friend_id']. "<br>";
$friend_id = $row['friend_id'];
$friend_id = mysql_real_escape_string($friend_id);
//accessing user_info table to access user info
$picquery = "SELECT * FROM user_info WHERE userid = '$friend_id'";
$picresult = mysql_query($picquery);
$pic = mysql_fetch_assoc($picresult);
$row_array['name'] = $pic['name'];
$row_array['picurl'] = $pic['picurl'];
$query2 = "SELECT * FROM user_post WHERE userid = '$friend_id'";
$result2 = mysql_query($query2);
//Access Posts Against userids
while( $row = mysql_fetch_array($result2) ) {
$post_id = $row['post_id'];
//for number of likes
$likesQuery = "SELECT COUNT(*) as totallikes FROM post_likes WHERE post_id = '$post_id'";
$likesResult = mysql_query($likesQuery);
$likesvalues = mysql_fetch_assoc($likesResult);
$num_of_likes = $likesvalues['totallikes'];
//for number of comments
$commentsQuery = "SELECT COUNT(*) as totalcomments FROM post_comments WHERE post_id = '$post_id'";
$commentsResult = mysql_query($commentsQuery);
$commentsvalues = mysql_fetch_assoc($commentsResult);
$num_of_comments = $commentsvalues['totalcomments'];
$row_array['post_id'] = $row['post_id'];
$row_array['userid'] = $row['userid'];
$row_array['post_text'] = $row['post_text'];
$row_array['post_time'] = $row['post_time'];
$row_array['post_num_likes'] = $num_of_likes;
$row_array['post_num_comments'] = $num_of_comments;
array_push($return_arr['newsfeed'],$row_array);
}
}
date_default_timezone_set('Asia/Karachi');
$date = date(' h:i:s a d/m/Y', time());
echo json_encode($return_arr,JSON_UNESCAPED_SLASHES);
something like this could help. As I don't have any test data I cannot see how it's working. It should display the post_id along with the count of the likes and the comments
SELECT
p.post_id,
COUNT(c.post_id) AS comments_count,
COUNT(l.post_id) AS like_count
FROM user_post p,
user_likes l,
user_comments c
WHERE p.post_id = l.post_id
AND p.post_id = c.post
GROUP BY p.post_id

SQL - How do i use a selected ID in another table?

SQL TABLES:
user = ID, name, email..
hero = ID, name, level..
owner = userid, heroid
I have these two queries:
$result = mysql_query("SELECT heroid FROM owner WHERE userid='$id'");
$heroid=mysql_fetch_row($result);
$result2 = mysql_query("SELECT name FROM hero WHERE ID = 'heroid[0]'");
$heroname = mysql_fetch_row($result2);
echo "User $id has a character named $heroname[0]";
$heroname = mysql_fetch_row($result2);
echo "User $id has a character named $heroname[0]";
..
Can this be done with a single query?
Yes. you can do:
SELECT h.name
FROM hero h
INNER JOIN owner o ON o.heroid = h.id
WHERE o.userid = $id

Is this query for selecting MUTUAL FRIEND is CORRECT?

<?php
mysql_connect("localhost","root","");
mysql_select_db('db2012');
$uid = 8;
$mid = 10;
$q = mysql_query("select friend from users_friends where user = $uid") or die(mysql_error());
if(mysql_num_rows($q) > 0)
{
while($r=mysql_fetch_array($q))
{
$qq = mysql_query("select friend from users_friends where user = $mid") or die(mysql_error());
while($rr = mysql_fetch_array($qq))
{
if($r[friend]==$rr[friend])
{
$friend_name_query = mysql_query("select name from users where uid = '$rr[friend]'") or die();
$friend_name = mysql_fetch_array($friend_name_query);
echo $friend_name[name];
}
}
}
}
?>
This query is working but any other way out to use less queries ? i m a beginner programmer please let me know if there is n e thng ...
You should use just one query:
$query = 'SELECT u.name
FROM users_friends f1
INNER JOIN users_friends f2 ON (f2.friend = f1.friend)
INNER JOIN user u ON (u.uid = f2.friend)
WHERE f1.user = "'. $uid. '"
AND f2.user = "'. $mid .'"';
run the query
$q = mysql_query($query) or die(mysql_error());
and do the while loop
while($r=mysql_fetch_array($q)){
echo $r['name'];
}
You only need the if(mysql_num_rows($q) > 0) statement if you do an else, otherwise you can just use the while
You could try below in order to get mutual friends of user 8 and 10.
select * from
user_friends a inner join user_friends b on
a.friend=b.friend and (a.user='8' and b.user='10')
U could then join the user table to get the name.

Categories