2

I am getting a bunch of id's from the database - for each id, I want to do a mysql query to get the relating row in another table. This works fine although I also need to get similiar data for the logged in user. With the mysql query I am using - it duplicates the logged in user data according to how many id's it originally pulled. This makes sense although isnt what I want. I dont want duplicate data for the logged in user and I need the data to come from one query so I can order things with the timestamp.

<?php
    mysql_select_db($database_runner, $runner);
    $query_friends_status = "SELECT DISTINCT rel2 FROM friends WHERE rel1 = '" . $_SESSION    ['logged_in_user'] . "'";
    $friends_status = mysql_query($query_friends_status, $runner) or die(mysql_error());
    $totalRows_friends_status = mysql_num_rows($friends_status);

    while($row_friends_status = mysql_fetch_assoc($friends_status))
    {
        $friends_id = $row_friends_status['rel2'];
        mysql_select_db($database_runner, $runner);
        $query_activity = "SELECT * FROM activity WHERE user_id = '$friends_id' OR user_id = '" .  $_SESSION['logged_in_user'] . "' ORDER BY `timestamp` DESC LIMIT 15";
        $activity = mysql_query($query_activity, $runner) or die(mysql_error());
        $totalRows_activity = mysql_num_rows($activity);
        echo "stuff here";
    }
?>
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
Jeff
  • 947
  • 2
  • 9
  • 23
  • Use of the php mysql_ functions are not recommended, please look into the mysqli_ functions this is a newer, faster and more secure way of doing mysql queries in php. – Dave_Peachy Jun 12 '12 at 16:18
  • 1
    You should probably be using a JOIN statement, you should also probably be using mysqli or PDO and not mysql_*. – Tim Withers Jun 12 '12 at 16:18
  • 3
    Your code also suffers from the [N+1 Problem](http://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem). – Jason McCreary Jun 12 '12 at 16:19
  • cool, i didn't know the name of this bad implementation :-) – Sebas Jun 12 '12 at 16:23
  • Thanks guys - Shredder got me down to one query. Will look into mysqli – Jeff Jun 12 '12 at 20:13

3 Answers3

1

You can try this single query and see if it does what you need

$userID = $_SESSION['logged_in_user'];
"SELECT * FROM activity WHERE user_id IN (
    SELECT DISTINCT rel2 FROM friends 
    WHERE rel1 = '$userID')
OR user_id = '$userID' ORDER BY `timestamp` DESC LIMIT 15";
Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
0

You probably want something like this:

  $user = $_SESSION['logged_in_user'];
  $query_friends_status = "SELECT * FROM friends, activity WHERE activity.user_id = friends.rel2 AND rel1 = '$user' ORDER BY `timestamp` DESC LIMIT 15";

You can replace * with the fields you want but remember to put the table name before the field name like:

table_name.field_name

I hope this helps.

Dave_Peachy
  • 498
  • 3
  • 12
0
 <?php
    require_once "connect.php";

    $connect = @new mysqli($host, $db_user, $db_password, $db_name); 
    $result = $connect->query("SELECT p.name, p.user, p.pass, p.pass_date_change,p.email, p.pass_date_email, d.domain_name, d.domain_price, d.domain_end, d.serwer, d.staff, d.positioning, d.media FROM domains d LEFT JOIN persons p 
    ON d.id_person = p.id AND d.next_staff_id_person != p.id");

    if($result->num_rows > 1)
    {   
    while($row = $result->fetch_assoc())
    {

echo '<td class="box_small_admin" data-column="Imię i nazwisko"><div class="box_admin">'.$row["name"].'</div></td>';
echo '<td class="box_small_admin" data-column="Domena"><div class="box_admin">'.$row["domain_name"].'</div></td>';          
}}
?>
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
inomi13
  • 1
  • 1
  • Welcome to SO! When you reply to a question, don´t just post code, please, explain it a little bit. If there are additional answers, try to expose the Pros of your code. – David García Bodego Oct 28 '19 at 03:47