First, thanks for the feedback!
Second, I do actually have a users table with their id being the primary key.
I turned the owner column in the pets table into an int and added a foreign key linking to the id column in the users table at your suggestion.
So here's what the tables look like:
Users:
id (primary key) - name
1 - KenKenderson
2 - chocolat
Pets:
a_id (primary key) - owner (foreign key)
1 - 1
2 - 2
3 - 1
I then instituted this query and it worked great!
PHP Code:
if (isset($_SESSION['user_id'])) {
$sql = "SELECT id,name,lastactive FROM users WHERE id='".$_SESSION['user_id']."'";
$query = mysql_query($sql);
$row = mysql_fetch_object($query);
$dbid = htmlspecialchars($row->id);
$username = htmlspecialchars($row->name);
$lastactive = htmlspecialchars($row->lastactive);
echo "<h1>$username</h1>";
echo "<p>Last active on: $lastactive</p>";
//display pets
$sql = "SELECT a_id,owner FROM pets WHERE owner = $dbid";
$query = mysql_query($sql);
$count = mysql_num_rows($query);
$i = 1;
while($row = mysql_fetch_object($query)) {
$a_id = htmlspecialchars($row->a_id);
$owner = htmlspecialchars($row->owner);
if($owner == $dbid) {
echo "<img src=\"".$a_id.".jpg\">";
}
$i++;
}
}
This code prints out the username, last active date, and pets of the logged in user.
Thanks so much for your help!
Bookmarks