2

I'm trying to retrieve the results of users in my database, and then if my checkbox is selected to retrieve only users who have photos to show those results, but I can't seem to figure out how to loop through those results or if I am even doing it in the right context. `

$photos = $_POST['pcbox'];
$basicsql = "SELECT * FROM users";
$basicsql .= "WHERE status > '1'";
if($photos=='1'){
    $sql = mysql_query("SELECT user_id FROM pictures GROUP BY user_id");
    while($row2 = mysql_fetch_assoc($sql))
    $options[] = " AND (users.user_id = '$row2[user_id]')";
    foreach($options as $key => $str){
        $basicsql .= $str;
    }
}
$basicsql .= " ORDER BY users.last_login DESC";
$pagesql = mysql_query($basicsql);

All works until the checkbox is selected

5
  • Is there really a quote missing at the end of the 3rd line ? Or is it really your code ? I'm not sure if I should correct that. Commented May 4, 2011 at 16:24
  • I forgot it on placing this example.
    – Bobby
    Commented May 4, 2011 at 16:27
  • Unfortunately it's too short a fix to get past the minimum edit length!
    – Phil Lello
    Commented May 4, 2011 at 16:29
  • @PhilLello: Then I'm not sure how I just edited it..? Commented May 4, 2011 at 16:33
  • @Tomalak Must be different for peer-review-edits and full-edits.
    – Phil Lello
    Commented May 4, 2011 at 16:36

4 Answers 4

1

You can't use the same db handle for nested loops. You need to explicitly set the db handle for each one.

However, you can just do it in one sql query.

Observe:

Select * from users u inner join pictures p on u.id=p.user_id where status > 1

An INNER JOIN will give you only users that have rows in the pictures table.

This will give you users that have pictures only.

Edit

This will give the number of pitcures and only 1 user row.

Select name,email,address,status,count(*) as num_pics from users u inner join pictures p on u.id=p.user_id group by name,email,address,status where status > 1

5
  • I've tried that way, but then if the user has more then one picture it repeats the user as many times has they have pictures.
    – Bobby
    Commented May 4, 2011 at 16:22
  • Ok, well I ended up figuring it out by just adding if($photos=='1'){ $basicsql .= ""; } a couple times through out the areas I needed to achieve this.
    – Bobby
    Commented May 4, 2011 at 16:36
  • @Bobby: $basicsql .= ""; doesn't do anything. Commented May 4, 2011 at 16:38
  • Yeah I know, what I ended up doing was... $basicsql = "SELECT *,users.user_id FROM users"; if($photos=='1'){ $basicsql .= ", pictures"; } $basicsql .= " WHERE status > '1' if($photos=='1'){ $basicsql .= " AND (users.user_id = pictures.user_id)"; $basicsql .= " GROUP BY users.user_id"; } $basicsql .= " ORDER BY users.last_login DESC"; $pagesql = mysql_query($basicsql);
    – Bobby
    Commented May 4, 2011 at 16:53
  • I'd use my solution because it is more efficient and simpler to understand. Good luck! Commented May 4, 2011 at 17:13
0

Not exactly sure if this is what you're looking for. Untested as well.

$photos = $_POST['pcbox'];
$basicsql = "SELECT * FROM users WHERE status > 1";
$photoSql = "SELECT * FROM users, pictures WHERE status > 1 AND users.user_id = pictures.user_id GROUP BY users.user_id ORDER BY users.last_login DESC"

if($photos=='1')
{
    $result = mysql_query($photosSql);
}
else
{
    $result = mysql_query($photoSql);
}
2
  • You can't select * and only group by user.id Commented May 4, 2011 at 16:28
  • You meant $basicsql in the second clause. Commented May 4, 2011 at 16:34
0
$photos = isset($_POST['pcbox']) ? $_POST['pcbox'] : null;

$sql = 'SELECT U.* FROM users AS U LEFT JOIN pictures AS P ON P.user_id = U.id ' . ($photos == '1' ? 'WHERE P.id IS NOT NULL') . ' GROUP BY U.id ORDER BY U.last_login DESC';

$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
  // Do something with the user info, such as outputting it to a table
}
-1

Furthermore, close while before start looping in options.

2

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.