PDA

View Full Version : Showing data from 2 tables


misterfade
01-25-2006, 01:42 PM
I need help with some code. I have a special usergroup that only they can access a certain page. I want to set it up so that when they login, it displays their info from the USER table, and a custom table I created called TEST. Here's what I have so far:


<?php
if ($vbulletin->userinfo['usergroupid']==6)
{
$result = $vbulletin->db->query_read("SELECT * FROM test, user WHERE test.test_ID = user.userid");
while ($row = $vbulletin->db->fetch_array($result))
{
echo $row['test_name'];
}
}
?>


So basically I need to match their ID from the TEST table to the USER table, and display the relevant data. I know it's probably something simple, it's just that the Vbulletin query tags are screwing me up lol.

Thanks.

Andreas
01-25-2006, 02:01 PM
I've left out the user table as this info is already available in $vbulletin->userinfo, so the JOIN is avoided -> lighter query.


<?php
if ($vbulletin->userinfo['usergroupid']==6)
{
$result = $vbulletin->db->query_read("SELECT * FROM test WHERE test_ID = " . $vbulletin->userinfo['userid']);
while ($row = $vbulletin->db->fetch_array($result))
{
echo $row['test_name'];
}
}
?>


Also, if there is only one record for ewach userid it could be simpilified to

<?php
if ($vbulletin->userinfo['usergroupid']==6 )
{
$row = $vbulletin->db->query_first("SELECT * FROM test WHERE test_ID = " . $vbulletin->userinfo['userid']);
echo $row['test_name'];
}
?>

misterfade
01-25-2006, 02:18 PM
Beautiful! It works great now. Thanks.