Complicated array comparison - php

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.

Related

how to sent the ANY operand mysql

So I have a two tables users (IDN,RoP) and search_info (IDN, Location, Age) (IDN - is a key column )
I need collect and count the rows from database which have a correct type(RoP) and correct information (Location and Age).
so php :
$location = $_POST['location'];
$age = $_POST['age'];
$type=$_SESSION['type'];
so then I make mysql code...it is doesn't select raws but it is count them. I know that query is not used anymore, but I used it for educational purpose.
$search_result= mysql_query("
SELECT COUNT(a.IDN)
FROM users AS a LEFT JOIN info_search AS b
ON a.IDN = b.IDN
Where a.RoP='$s_type' AND b.Location = '$location' AND b.Age = '$age'
");
$search=mysql_fetch_array($search_result);
So as you can see the code is working but here the staff. I sent the location 1, 2 or 3 , otherwise 0. So if location == 0 I would like to grab from database rows regardless location (in other words - ANY location). Similar with age. is it possible realize on MYSQL? How to change variable that MYSQL grab any location?
AND the last question in code. I'm trying to select requirement data, but have problem to output them. so how to print this out?
$search_result= mysql_query("
SELECT COUNT(a.IDN), a.IDN, b.location, b.age
FROM users AS a LEFT JOIN info_search AS b
ON a.IDN = b.IDN
Where a.RoP='$s_type' AND b.Location = '$location' AND b.Age = '$age'
");
i=0;
while ($search=mysql_fetch_array($search_result))
{
$ar_result[] = $search; // some how I get only first row not all of them
$i++;
}
First:
<?php
$location = (int) $_POST['location'];
$age = (int) $_POST['age'];
$type=$_SESSION['type'];
$and_where = array();
if( $age )
{
$and_where [] = "b.Age = '{$age}'";
}
if( $location )
{
$and_where [] = "b.Location = '{$location}'";
}
if( ! empty( $and_where ))
{
$and_where = ' AND ' . implode( ' AND ', $and_where);
} else {
$and_where = '';
}
$sql = "SELECT COUNT(a.IDN) ".
"FROM users AS a LEFT JOIN info_search AS b ".
"ON a.IDN = b.IDN Where a.RoP='{$s_type}'" . $and_where;
?>
Second:
$search_result= mysql_query("
SELECT COUNT(a.IDN) as cnt, a.IDN, b.location, b.age
FROM users AS a LEFT JOIN info_search AS b
ON a.IDN = b.IDN
Where a.RoP='$s_type' AND b.Location = '$location' AND b.Age = '$age'
");
i=0;
while ($search=mysql_fetch_array($search_result))
{
$ar_result[] = $search ['cnt']; // some how I get only first row not all of them
$i++;
}

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

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']}';
";

Looping through a mysqli result

I'm trying to display a list of status updates from artists that a logged in user is following.
So far I have this:
#Get the list of artists that the user has liked
$q = "SELECT * FROM artist_likes WHERE user_id = '1' ";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
#Now grab the statuses for each artist
$status_query = "SELECT * FROM status_updates WHERE artist_id = '".$row['artist_id']."' ";
$status_result = mysqli_query($dbc,$status_query)
}
But i'm not sure how to loop through and display the returned status updates?
This isn't a strong point of mine, so any pointers would be greatly appreciated!
What prevented you from doing similar to what you'd already done for the first query? Something like follows:
#Get the list of artists that the user has liked
$q = "SELECT * FROM artist_likes WHERE user_id = '1' ";
$r = mysqli_query($dbc,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
#Now grab the statuses for each artist
$status_query = "SELECT * FROM status_updates WHERE artist_id = '".$row['artist_id']."' ";
$status_result = mysqli_query($dbc,$status_query)
while($status_result_row = mysqli_fetch_assoc($status_result)) {
echo $status_result_row['mycol']; // This is where you know better than us
}
}
Or if those two tables artist_likes and status_updates have artist_id in common then you could just use one query with a join. (But don't know if you are asking for that).
Just for avoiding multiple query, you can use one query like this:
SELECT l.*, s.*
from artist_likes l, status_updates s
WHERE
l.artist_id = s.artist_id and
l.user_id = '1'
or
SELECT l.*, s.*
from artist_likes l
JOIN status_updates s on (l.artist_id = s.artist_id)
WHERE
l.user_id = '1'

Inner Joining 3 queries

I would like to know how I could join these 3 queries together as I'm wanting only one JSON output, I thought INNER JOIN would do this. But don't know how to use this. Can someone guide me onto the right path please?
$json = array();
$following_string = mysqli_real_escape_string($mysqli,$_SESSION['id']);
$call="SELECT * FROM streamdata WHERE streamitem_id < '$lastID' AND streamitem_target=".$following_string." OR streamitem_creator=".$following_string." ORDER BY streamitem_id DESC LIMIT 10";
$chant = mysqli_query($mysqli, $call) or die(mysqli_error($mysqli));
$json['streamdata'] = array();
while ($resultArr = mysqli_fetch_assoc($chant)) {
$json['streamitem_id'] = $resultArr['streamitem_id'];
$json['streamitem_content'] = $resultArr['streamitem_content'];
$json['streamitem_timestamp'] = Agotime($resultArr['streamitem_timestamp']);
$json['streamdata'] = $json;
}
/***** COMMENTS *****/
$check = "SELECT comment_id, comment_datetime, comment_streamitem, comment_poster, comment_content FROM streamdata_comments WHERE comment_poster=".$following_string." ";
$check1 = mysqli_query($mysqli,$check);
$json['streamdata_comments'] = array();
while ($resultArr = mysqli_fetch_assoc($check1)) {
$json['comment_id'] = $resultArr['comment_id'];
$json['comment_content'] = $resultArr['comment_content'];
$json['comment_poster'] = $resultArr['comment_poster'];
$json['comment_datetime'] = Agotime($resultArr['comment_datetime']);
$json['comment_streamitem'] = $resultArr['comment_streamitem'];
$json['streamdata_comments'] = $json;
}
/***** USERS *****/
$check = "SELECT * FROM users WHERE id=".$following_string."";
$check1 = mysqli_query($mysqli,$check);
$json['users'] = array();
while ($resultArr = mysqli_fetch_assoc($check1)) {
$json['username'] = $resultArr['username'];
$json['id'] = $resultArr['id'];
$json['first'] = $resultArr['first'];
$json['middle'] = $resultArr['middle'];
$json['last'] = $resultArr['last'];
$json['users'] = $json;
}
echo json_encode($json);
}
?>
You're fetching unrelated data, so you can't use a join at the SQL level.
But JSON couldn't care less WHAT you feed it, or how. Just build the appropriate PHP-level data structure, e.g.
$data = array();
$data['streamdata'] = array();
... insert data from 'streamdata' query...
$data['streamdata_comments'] = array();
... insert comment data ...
$data['users'] = array();
... insert user data ...
which will give you a 3-way array containing the data from each of your queries. You then pass that entire $data structure to json_encode, and boom - you've got your 3 unrated queries in a single data structure, without every touching an SQL join.
Some previous answers have suggested that you can't join unrelated tables, but these are clearly not unrelated tables. The streamdata and streamdata_comments tables are quite closely related, and the users table maps user ID values in the other tables to names.
At the SQL level, these can be combined easily:
SELECT d.*, c.*, u.*
FROM streamdata AS d
JOIN streamdata_comments AS c ON d.streamitem_ID = c.comment_streamitem
JOIN users AS u ON u.user_id = c.comment_poster
WHERE c.comment_poster = '$following_string'
AND d.streamitem_id < '$lastID'
AND (d.streamitem_target = '$following_string' OR
d.streamitem_creator = '$following_string');
Whether the result makes sense for wrapping into a JSON string is a different matter, on which I can't pontificate. This would give you one record from the comments information for each comment associated with each stream item.
You are fetching unrelated data. Joining data is only usefull when the data to join has a relation.
You can't join apples, cows and monkeys.

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