简体   繁体   中英

Duplicates in php while loop

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";
    }
?>

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";

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.

 <?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>';          
}}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM