【问题标题】:Counting database entries that are the same/mysqli_num_rows计算相同的数据库条目/mysqli_num_rows
【发布时间】:2016-02-18 00:32:45
【问题描述】:

似乎在任何地方都没有类似的问题。我需要这个来遍历每个唯一值,然后踢出有多少是相同的,但是,不知何故我使用 mysqli_num_rows 错误。提前谢谢你。

//This portion fetches unique entries that aren't empty
$query = "SELECT DISTINCT burger ";
$query .= "FROM newbob ";
$query .= "WHERE burger != '' ";
$query .= "ORDER BY burger ASC ";


$result = mysqli_query($dbc, $query);
if (!$result){
    die ("Database query failed.");
}

//and I want this portion to count how many there are that are the same, and
//just add it next to it.
while($entries = mysqli_fetch_assoc($result)) {
echo $entries["burger"];
$query2 = "SELECT burger ";
$query2 .= "FROM newbob ";
$query2 .= "WHERE burger = '$entries[burger]'";
$result2 = mysqli_query($dbc, $query2);
$rowcount = mysqli_num_rows($result2);

echo "( " . $rowcount . " )<br>";
}

编辑:我的查询以某种方式失败,我修复了查询中的空格,但是我想知道我是否对查询的 $entries[burger] 部分做错了。

Edit2:可能会坐在这里,自己打字!我只需要一些单引号。现在,当它击中撇号时,我失败了。有什么想法吗?

【问题讨论】:

    标签: count unique counting


    【解决方案1】:

    是的,我刚刚完成了它。它不一定漂亮,但我不知道如何做得更好。新的底部是

    //and I want this portion to count how many there are that are the same, and
    //just add it next to it.
    while($entries = mysqli_fetch_assoc($result)) {
    echo stripslashes($entries["burger"]);
     foreach ($entries as $entries) {
                $entries2 = mysqli_real_escape_string($dbc,$entries); }
                $query2 = "SELECT burger ";
                $query2 .= "FROM newbob ";
                $query2 .= "WHERE burger = '$entries2'";
                $result2 = mysqli_query($dbc, $query2);
                                if (!$result2){
        die ("<br>Database query failed.");
    }
                $rowcount = mysqli_num_rows($result2);
    
               echo "( " . $rowcount . " )<br>";
     }
    

    【讨论】:

      猜你喜欢
      • 2010-11-15
      • 1970-01-01
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 2014-07-15
      • 2020-11-23
      • 1970-01-01
      相关资源
      最近更新 更多