-5

How can dispaly all of the tutorial_post and the username of the user who posted the post... i wanna see how the sql statement would look like

enter image description here enter image description here

yvensup
  • 75
  • 1
  • 3
  • 8
  • 1
    Any effort? [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Lion Jun 13 '13 at 17:49
  • Try doing some research on how to peform basic MySQL queries. I.e. http://dev.mysql.com/doc/refman/5.0/en/select.html or https://www.google.co.uk/search?q=mysql+query+beginners – Simon at The Access Group Jun 13 '13 at 17:52

1 Answers1

0

You can make something like this:

$postID = 1;//Post id we want to get from the database
$getPost = $connection->query("SELECT * FROM posts WHERE post_id='$postID'");//Get the post by the id
$post = $getPost->fetch_assoc();//Fetch the result to an array
$getUser = $connection->query("SELECT username FROM users WHERE user_id=".$post['id']);//Get the username by using the id we got from the $post['user_id']
$user = $getUser->fetch_assoc();//Fetch the second result to an array

//Print everything
echo "Title:".$post['title']."<br/>";
echo "Username:".$user['username']."<br/>";
echo "Body:".$post['body'];

I can't really see the names of the tables, so i have just guessed them.

Orel Biton
  • 3,478
  • 2
  • 15
  • 15
  • This is chock full of SQL injection possibilities. – nhinkle Jun 13 '13 at 19:36
  • I know that, but as i can see here he is a beginner and i didn't wanna make it too hard for him, ofcourse you're able to use `real_escape_string` and put the variables inside curly brackets. – Orel Biton Jun 13 '13 at 19:38
  • 1
    I don't think it's a good idea to start off beginners with insecure code. @yvensup you should read [how to prevent SQL injection in PHP](http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php) before you start writing your code. Not only will it teach you to write secure queries from the get-go, it will also give you a good starting point for how to write queries in general. – nhinkle Jun 13 '13 at 19:46