Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从MySQL表中返回每个唯一的用户,以及每个用户的行数计数_Php_Mysql_Select_While Loop - Fatal编程技术网

Php 从MySQL表中返回每个唯一的用户,以及每个用户的行数计数

Php 从MySQL表中返回每个唯一的用户,以及每个用户的行数计数,php,mysql,select,while-loop,Php,Mysql,Select,While Loop,我使用以下MySQL查询为数据库中的用户生成一个表。查询被设计为只为每个用户返回一行,即使每个用户有多行。这很好,但是我还需要计算每个用户的唯一条目的数量,以便输入表中,在表中声明此处。我是否需要使用另一个查询来返回所有条目的计数,如果需要,如何将其与我已有的代码集成 $query=“从用户中选择,计数(*)作为曲目组中的num,从用户按计数排序(*)描述”; $result=mysql\u query($query)或die(mysql\u error()); while($row=mysq

我使用以下MySQL查询为数据库中的用户生成一个表。查询被设计为只为每个用户返回一行,即使每个用户有多行。这很好,但是我还需要计算每个用户的唯一条目的数量,以便输入表中,在表中声明此处。我是否需要使用另一个查询来返回所有条目的计数,如果需要,如何将其与我已有的代码集成

$query=“从用户中选择,计数(*)作为曲目组中的num,从用户按计数排序(*)描述”;
$result=mysql\u query($query)或die(mysql\u error());
while($row=mysql\u fetch\u数组($result)){
$user=$row['from_user'];
回声“;
回显“$user.”;
echo“上传(**此处**)”;
回声“收藏(计数)”;
回声“;
}
?>

因为您已经创建了自定义字段“num”,所以可以使用它来获取计数

user=…

$count = $row['num'];
那你可以

echo "<td>uploads ($count)</td>";
echo“上传($count)”;

知道字段名会错过表结构,但是,如果我完全理解您的问题,您可以在mysql中使用count+。 你也可以查一下

对于第二个问题,您可以执行第二个查询,或者执行连接跟踪

我认为,在您的情况下,在循环中进行第二次查询更容易从“用户”结果中获得所有细节

$query1="SELECT DISTINCT(from_user), COUNT(*) AS num 
FROM tracks 
GROUP BY from_user 
ORDER BY COUNT(*) DESC";

$query2="SELECT * FROM tracks";

$result1=mysql_query($query1) or die(mysql_error());
$result2=mysql_query($query2) or die(mysql_error());

$user_array = array();

while ($row = mysql_fetch_array($result1)) {

    $user = $row['from_user'];
    $num = $row['num'];

    $uploads_array = array();

    while ($sub_row = mysql_fetch_array($result2)) { 
      if( $sub_row['from_user'] == $user ) {
            //for example only due to the unknown structure of your table
            $uploads_array[] = array( 
                "file_name" => $sub_row['file_name'], 
                "file_url" => $sub_row['file_url'] 
            ); 
      }
    }

    $user_array[] = array( 
        "name" => $user, 
        "num_entry" => $num, 
        "actions" => $uploads_array
    );

}

// now the table with all data is stuctured and you can parse it

foreach($user_array as $result) {
    $upload_html_link_arr = array();
    $user = $result['name'];
    $num_entry = $result['num_entry'];
    $all_actions_from_user_array = $result['actions'];

    foreach($all_actions_from_user_array as $upload) { 
        $upload_html_link_arr[] = sprintf('<a href="%s">%s</a>', $upload["file_url"],$upload["file_name"]);
    }

    $upload_html_link = implode(', ',$upload_html_link_arr);

    $full_row = sprintf("<tr><td>%s</td><td>uploads : %s</td><td>favourites (%d)</td></tr>", $user, $upload_html_link, $num_entry);

    // now just echo the full row or store it to a table for the final echo.
    echo $full_row;
}
$query1=“选择不同的(来自用户),将(*)计数为num
从轨道
按用户分组
按计数排序(*)说明”;
$query2=“从曲目中选择*”;
$result1=mysql\u query($query1)或die(mysql\u error());
$result2=mysql\u query($query2)或die(mysql\u error());
$user_array=array();
而($row=mysql\u fetch\u数组($result1)){
$user=$row['from_user'];
$num=$row['num'];
$uploads_array=array();
而($sub_row=mysql_fetch_数组($result2)){
if($sub_行['from_user']=$user){
//例如,仅由于表的结构未知
$uploads_数组[]=数组(
“文件名”=>$sub\u行[“文件名”],
“文件url”=>$sub\u行['file\u url']
); 
}
}
$user_array[]=数组(
“name”=>$user,
“num_entry”=>num美元,
“操作”=>$uploads\u数组
);
}
//现在,包含所有数据的表已经构建好,您可以解析它了
foreach($user\u数组作为$result){
$upload\u html\u link\u arr=array();
$user=$result['name'];
$num_entry=$result['num_entry'];
$all_actions_from_user_array=$result['actions'];
foreach($用户数组中的所有\u操作\u作为$upload){
$upload_html_link_arr[]=sprintf(“”,$upload[“文件url”],$upload[“文件名”]);
}
$upload\U html\U link=内爆(“,”,$upload\U html\U link\U arr);
$full\u row=sprintf(“%suploads:%sfavourites(%d)”,$user,$upload\u html\u link,$num\u entry);
//现在,只需回显整行或将其存储到表中以进行最终回显。
echo$full_行;
}

我希望这能帮上忙,迈克

你现在数的到底是什么?我的意思是:表'tracks'中有什么?@MartyMcVry表'tracks'包含大约12列,我想计算每个用户的条目数,即'from_user'列中的每个唯一字符串('nick','simon',等等)。
SELECT DISTINCT(from_user) AS user, 
COUNT(from_user) AS num 
FROM tracks 
GROUP BY from_user 
ORDER BY num DESC";
$query1="SELECT DISTINCT(from_user), COUNT(*) AS num 
FROM tracks 
GROUP BY from_user 
ORDER BY COUNT(*) DESC";

$query2="SELECT * FROM tracks";

$result1=mysql_query($query1) or die(mysql_error());
$result2=mysql_query($query2) or die(mysql_error());

$user_array = array();

while ($row = mysql_fetch_array($result1)) {

    $user = $row['from_user'];
    $num = $row['num'];

    $uploads_array = array();

    while ($sub_row = mysql_fetch_array($result2)) { 
      if( $sub_row['from_user'] == $user ) {
            //for example only due to the unknown structure of your table
            $uploads_array[] = array( 
                "file_name" => $sub_row['file_name'], 
                "file_url" => $sub_row['file_url'] 
            ); 
      }
    }

    $user_array[] = array( 
        "name" => $user, 
        "num_entry" => $num, 
        "actions" => $uploads_array
    );

}

// now the table with all data is stuctured and you can parse it

foreach($user_array as $result) {
    $upload_html_link_arr = array();
    $user = $result['name'];
    $num_entry = $result['num_entry'];
    $all_actions_from_user_array = $result['actions'];

    foreach($all_actions_from_user_array as $upload) { 
        $upload_html_link_arr[] = sprintf('<a href="%s">%s</a>', $upload["file_url"],$upload["file_name"]);
    }

    $upload_html_link = implode(', ',$upload_html_link_arr);

    $full_row = sprintf("<tr><td>%s</td><td>uploads : %s</td><td>favourites (%d)</td></tr>", $user, $upload_html_link, $num_entry);

    // now just echo the full row or store it to a table for the final echo.
    echo $full_row;
}