简体   繁体   中英

PHP - Count amount of rows joined

I'm having the following code:

I wan't to count how many Answers there is to a specific Thread/lookup. Like, how many replied to this lookup.

function lookup()
{
    $sql = "SELECT * FROM lookup
            INNER JOIN lookupCategories
            ON lookup.FK_lookupCategory=lookupCategories.lookupCategoriesId
            INNER JOIN lookupType
            ON lookup.FK_lookupType=lookupType.lookupTypeId
            LEFT JOIN authentication
            ON lookup.FK_lookupUserId=authentication.userId
            LEFT JOIN freelanceTypes
            ON lookup.FK_freelanceTypes=freelanceTypes.freelanceTypesId
            LEFT JOIN lookupAnswer
            ON lookup.lookupId=lookupAnswer.FK_lookupId
            ORDER BY lookupId DESC
            LIMIT 5";
    $result = mysql_query($sql)or die(mysql_error());

    $viewLookup = Array();

    while($row=mysql_fetch_assoc($result)):

        $viewLookup[$row['lookupId']]['lookupId'] = $row['lookupId'];
        $viewLookup[$row['lookupId']]['lookupHeader'] = $row['lookupHeader'];
        $viewLookup[$row['lookupId']]['lookupSubHeader'] = $row['lookupSubHeader'];
        $viewLookup[$row['lookupId']]['lookupContent'] = $row['lookupContent'];
        $viewLookup[$row['lookupId']]['lookupDate'] = $row['lookupDate'];
        $viewLookup[$row['lookupId']]['lookupCategory'] = $row['lookupCategoryName'];
        $viewLookup[$row['lookupId']]['lookupType'] = $row['lookupTypeName'];
        $viewLookup[$row['lookupId']]['lookupByUsername'] = $row['username'];
        $viewLookup[$row['lookupId']]['lookupByUserId'] = $row['userId'];
        $viewLookup[$row['lookupId']]['lookupFreelanceTypeId'] = $row['FK_freelanceTypes'];
        $viewLookup[$row['lookupId']]['freelanceWinner'] = $row['freelanceWinner'];

    endwhile;
    return $viewLookup;
}

And I wan't to count the amount of 'answers' to a thread. My threads are joined to the lookup's. And afterwards stored in an array as you see.

How would I count the answers, and store the number in the array together with the rest of my data? I have tried to do this, but can't figure out what's wrong. It's just returning if there's something or not - 1 or NULL.

$viewLookup[$row['lookupId']]['countAnswers'] = count($row['lookupAnswerId']);

Thanks in advance :)

$sql = select count(*)....

其余添加条件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM