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";
    }
?>
5
  • 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. Commented Jun 12, 2012 at 16:18
  • 1
    You should probably be using a JOIN statement, you should also probably be using mysqli or PDO and not mysql_*. Commented Jun 12, 2012 at 16:18
  • 3
    Your code also suffers from the N+1 Problem. Commented Jun 12, 2012 at 16:19
  • cool, i didn't know the name of this bad implementation :-) Commented Jun 12, 2012 at 16:23
  • Thanks guys - Shredder got me down to one query. Will look into mysqli Commented Jun 12, 2012 at 20:13

3 Answers 3

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";
Sign up to request clarification or add additional context in comments.

1 Comment

Stumbled across this, and it really helped with my query within query while loop issue i have. Thanks. +1
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.

2 Comments

Cheers Frank - the first suggestion worked but appreciate the help
Yeah the first suggestion looks better i would use that over mine :P
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>';          
}}
?>

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.