2017-10-05 53 views
-2

从“Pollcomment”表中计数poll_comment。从两个表中获取计数并加入存储

更新计数到调查表中“pollcomment”列

这是代码,我都试过了。

$asdf = mysqli_query($conn, "SELECT COUNT(*) FROM pollcomment WHERE pollid='$pollid'"); 
$datas = mysqli_num_rows($conn, $asdf); 
$results = "UPDATE polls SET PollCommentCount = '$datas' WHERE id = '$pollid'"; 

民意调查表

enter image description here

Pollcomment表

enter image description here

+2

你尝试过什么?在MySQL中有一个名为'count'的函数,不知道为什么PHP标记已被添加,但我已投票删除它。 – IsThisJavascript

+0

显示您尝试过的查询的示例。给我们一些东西来帮助你。 –

+0

@RobertDickey,我已经添加了我试过的代码。 – JRK

回答

0

做了一些edits--

我WOU ld建议采取两种不同的行动,以使不熟悉该语言的人更容易。你可以试试

select pollComment ,count(*) as count from students group by pollComment 

这将允许你得到每个ID的数量,如果这是你想要的。如果您只想获得单个ID的计数,请添加WHERE子句。

select pollComment ,count(*) as count from students group by pollComment where pollid='$pollid'; 

然后,你可以像你正试图与你的第二个查询更新。

这里是你在找什么作为一个整体,我相信

<?php 

    $query = "select pollComment ,count(*) as count from polls where pollid='$pollid' group by pollComment ;" 
if ($result = mysqli_query($conn, $query)) { 
/* fetch associative array */ 
while ($row = mysqli_fetch_assoc($result)) 

{ 
    $count = $row['count']; 

    $update = "UPDATE polls SET PollCommentCount = '$count' WHERE id = '$pollid'"; 
    if ($result = mysqli_query($conn, $update)) { 
     /* fetch associative array */ 
     while ($row = mysqli_fetch_assoc($result)) { 

     }} 

    } 
    /* free result set */ 
    mysqli_free_result($result); 
    } 

?> 
相关问题