计算相同的数据库条目/ mysqli_num_rows [英] Counting database entries that are the same/mysqli_num_rows

查看:317
本文介绍了计算相同的数据库条目/ mysqli_num_rows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何地方似乎都没有类似的问题。我需要这只是循环通过每个唯一的值,然后踢出多少是相同的,不知何故我使用mysqli_num_rows错误。

There doesn't seem to be a similar question anywhere. I need this to just loop through each unique value, and then kick out how many are the same but, somehow I'm using mysqli_num_rows wrong. Thank you in advance.

//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] 部分查询中做错了。

my query is failing somehow, I fixed the spaces in the query but, I'm wondering if I'm doing something wrong with the $entries[burger] part of the query.

Edit2:可以坐在这里,通过它输入自己!我只需要一些单引号。现在我失败了,当它打了一个撇号,虽然。任何想法?

Might sit here and type myself through it! I just needed some single quotes around it. Now I'm failing when it hit's an apostrophe though. Any ideas?

推荐答案

Yup,直接通过它。它不一定是美丽的,但我不知道如何做得更好。新的底部是

Yup, just worked my way right through it. It's not necessarily beautiful but, I'm not sure how to do it better. The new bottom portion is

//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>";
 }

这篇关于计算相同的数据库条目/ mysqli_num_rows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆