7
I have two tables in the database, one listing the posts and the other of registered users.
Columns I have in the table of posts:
ID || TEXTO || ID_USER ||
Columns I have in the user table:
ID || NOME_USER || SENHA_USER||
When making a post any system picks up the ID
of the user who is posting and logging the data.
Only that I have trouble working better with both tables, actually I want to make an association between both tables. Because I want to associate the ID_USER
registered in the table of posts and associate the ID
to the user’s name.
I tried to do that:
("SELECT * FROM posts WHERE ID_USER in (SELECT ID FROM users)")
But it didn’t work.
At the moment the posts displayed are like this:
3 said :: Text ID 3 user posted
But I want them to stay that way:
Carlos said :: Text ID 3 user posted
This is all PHP I’m using to display posts:
<?php
$rs = $pdo->query("SELECT * FROM posts ORDER BY ID DESC ")->fetchAll();
$count = count($rs);
if($count == 0){ echo "Nada foi encontrado"; }else{
if(!$rs){
print_r($pdo->errorInfo());
}foreach ($rs as $res)
{
?>
<?php echo $res['ID_USER'] ?> disse :: <?php echo $res['TEXTO'] ?>
<?php } ?>
inner join
wouldn’t it be better? Put the PHP you have– Papa Charlie
I read about Ner, but I can’t apply it correctly. php is very simple. I’ll edit the question and put the php that I’m using to display
– ivan veloso
[code review] Ivan, code indentation is essential to make logic clear and easy to understand. The functions
printf
andsprintf
are perfect to output small chunks of HTML and not have to keep opening and closing PHP. I would rewrite the code like this: http://pastebin.com/kYjPq3iV– brasofilo