mardi 28 mars 2017

Execute an SQL query inside another SQL query

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 = 'http://ift.tt/19xbBZH';
                $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.




Aucun commentaire:

Enregistrer un commentaire