4

Ok, I assume that the title is not the best title, but let me explain my problem: I'm creating a website that needs to show posts of people (anyway), and I have to show their gravatar's profile picture, so this what I did:

<?php 
            function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
                $url = 'https://www.gravatar.com/avatar/';
                $url .= md5( strtolower( trim( $email ) ) );
                $url .= "?s=$s&d=$d&r=$r";
                if ( $img ) {
                    $url = '<img src="' . $url . '"';
                    foreach ( $atts as $key => $val )
                        $url .= ' ' . $key . '="' . $val . '"';
                    $url .= ' />';
                }
                return $url;
            }
            require("db.php");
            $sql = "SELECT * FROM posts ORDER BY date DESC";
            foreach ($db->query($sql) as $row) {
                // var_dump($row);
                $user = $row['user_id'];
                $sql_user = "SELECT email FROM users WHERE id = $user";
                foreach ($db->$sql_user as $row_user) {
                    var_dump($row_user);
                    echo "<img src=\"".get_gravatar($row_user['email'])."\"/>";
                }
                echo "<h2>".$row['title']."</h2><br/>";
                echo "<p>".$row['content']."</p><br/>";
            }

But, it doesn't work (well, it works, but it doesn't shows me the profile picture of the user, only the post). So, I think the problem is that I can't call 2 times the variable $db at the same time, but I'm not sure, so that's m=why I'm asking if there is a way to fix my problem or to select 2 tables at the same time.

1
  • see your second foreach you miss to put query function Commented Mar 28, 2017 at 7:06

5 Answers 5

2

you can short your logic

$sql="SELECT u.email "
                    . "FROM posts AS p "
                    . "LEFT JOIN users AS u "
                    . "ON u.id=p.user_id "
                    . "ORDER BY p.date DESC";
Sign up to request clarification or add additional context in comments.

4 Comments

Could you develop? I mean, could you explain how I can use it?
This is better, as you won't be running multiple queries in your inner for loop, which adds overhead. Also I think it should be "SELECT p.user_id,u.email " in the first part. u and p mixed up.
just call the column you want, in you case is, $row['email'] , this is already relate in users data.also you can get $row['user_id'] in single result set
@waltron aw, your right, :) its mistake sample, or maybe we can remove taht, because only needed is email
1

use join

SELECT * FROM users join posts  on users.id =Posts.user_id ORDER BY date DESC

Comments

1

you don't apply query to sql string in second foreach

  foreach ($db->query($sql_user) as $row_user) {

2 Comments

Your solution works, but I prefer the join one, thanks :)
you prefer a solution that is a suggestion but not the solution for you question .. ? .. normally the soution is acceppeted .. and the suggestion are marked as useful ..
1

see your second foreach you miss to put query function

foreach ($db->query($sql) as $row) {



foreach ($db->query($sql_user) as $row_user) {

1 Comment

Your solution works, but I prefer the join one, thanks :)
1

You could just use JOIN

JOIN clause is used to combine rows from two or more tables, based on a related column between them.

7 Comments

join and Union are very different
@Jens Yeah they are different... UNION is used to combine the result from multiple SELECT statements into a single result set. while JOIN clause is used to combine rows from two or more tables, based on a related column between them... he could use any of this to achieve his result...he wanted to select 2 tables at the same time..
No he can not. You can not solve a Problem with very different functions
i was suggesting to use either one of this
Yes but if OP using Union, he get never the result he/she wants
|

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.