I have two tables...
Table 1 (keywordsTbl)
KeyID Key_Name
================================
1 Biology
2 Entertainment
Table 2 (profileTbl)
Keyword_ID Key_Name
================================
1(from keywordsTbl) Biology(from keywordsTbl)
2(from keywordsTbl) Entertainment(from keywordsTbl)
My query in profileTbl is indexing KeyID from the keywordsTbl and its working fine. The only problem is my output shows up like so (no array). :
Seems like its indexing only the 1st keyID... The results only show 1 single keyword even though there are more than one keyID being placed in the row:
My query looks as so:
include 'db.php';
$sql = mysqli_query($con,"SELECT * FROM profileTable, addKeywordTable ORDER BY LaName ASC");
while ($row = mysqli_fetch_array($sql))
{
$fname = $row['FirName'];
$lname = $row['LaName'];
$title = $row['Title'];
$keys = $row['Keyword_Name'];
$id = $row['source_ID'];
if(!in_array($title,array('',null)) && !empty($title)):
$title = '('.$title.')';
else:
$title = '';
endif;
What's the easiest way to show an array when two tables are being used? Would a join work better in this case?
I am not 100% sure that this is what you are asking, but if you want a list of results based on a join for KeyID
you can do something like this:
SELECT p.Laname, p.FiName, GROUP_CONCAT(k.Key_Name)
FROM ProfileTable AS p
JOIN addKeywordTable AS k ON k.KeyID = p.Keyword_ID
GROUP BY p.LaName, p.FiName
If you want to include more files in the select list you need to add them to the GROUP BY
list or use an aggregate function (GROUP_CONCAT
, MAX
, MIN
, etc) on them in the select list.