I've got 2 tables.
One is called 'dbUsers' the other 'posts'.
Usernames are stored in the 'dbUsers' table and are matched to an ID number. When users post a message their ID number is stored against a text field in the 'posts' table:
dbUsers:
ID Username
1 Bob
2 Dave
posts:
ID Text
1 Text text text text
2 Text text text text
When I output the text to a HTML page I want to display the username of the poster as well. I can display the user ID very easily but I want to check the ID number against the 'dbUsers' table and then display the username.
The code I have so far (see below) displays the Title, Text and user ID of the post. I want to replace the user ID with the Username:
<?php
include ("dbConfig.php");
mysql_select_db("ittechni_main");
// display individual record
if ($id) {
$result = mysql_query("SELECT * FROM posts WHERE id=$id");
$myrow = mysql_fetch_array($result);
printf("<font face=verdana size=2><b>%s</b>\n<br>", $myrow["title"]);
printf("<font face=verdana size=1>Posted by %s\n<br><br>", $myrow["user_id"]);
printf("<font face=verdana size=2>%s\n<br>", $myrow["text"]);
} else {
// show employee list
$result = mysql_query("SELECT * FROM posts");
if ($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
do {
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["title"], $myrow["text"]);
} while ($myrow = mysql_fetch_array($result));
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
?>