0

我不热衷于如何从数据库中创建一个数组。试图避免学习这些,但显然现在需要它。

基本上我的数据库是由数百万行构成的。Player Name 和 Snap(Unix 时间戳)通常是“重复的”,但在这些行(城市、x、y 等)中具有不同的数据。

理想情况下,试图弄清楚如何通过SELECT语句制作数组,例如:

输出,我想是这样的:Name = Snap =>(那天有多少个城市),Snap =>(另一天有多少个城市)..等等..

这里的问题是,什么是正确的数组“命令”(如果你这么称呼它)来提取名称 ONCE 并根据快照的计数提取多个快照。

我的选择是:

// SELECT count(*), player, snap
// FROM DB WHERE GROUP BY snap, player ORDER BY snap, player DESC
while ($row = fetch_assoc($Result) {
     // How you go about putting array here?
}
4

4 回答 4

2

它会像这样完成:

$Result = mysql_query("SELECT count(*) as count, player,
     snap FROM DB GROUP BY snap, player ORDER BY snap, player DESC");
while ($row = mysql_fetch_array($Result) {
  print_r( $row );
  echo "This is the count value -> " . $row['count'];
}
于 2012-04-26T08:40:13.953 回答
1

我同意关于需要阅读更多基础知识的评论,但您可以执行以下操作来创建您的多维数组:

<?php

$results = array();

$Result = 'SELECT count(*),player,snap FROM DB WHERE GROUP BY snap,player ORDER BY snap,player DESC';
while ($row = fetch_assoc($Result){
  if (!isset($results[$row['player']][$row['snap']]) {
    $results[$row['player']][$row['snap']] = 0;
  }
  $results[$row['player']][$row['snap']] += $row['count(*)']; 
}

?>
于 2012-04-26T08:45:12.980 回答
0

先试试这些:-

  1. http://phpsense.com/2006/php-mysql/
  2. http://www.freewebmasterhelp.com/tutorials/phpmysql

您还需要 SQL 基础知识。

于 2012-04-26T08:42:46.710 回答
0

你可以试试

$sql = "SELECT count(*) as count, player,
        snap FROM DB GROUP BY snap, player ORDER BY snap, player DESC";

/* Connect To Database */
$mysqli = new mysqli ( "localhost", "username", "password", "database" );

/* check connection */
if ($mysqli->connect_errno) {
    printf ( "Connect failed: %s\n", $mysqli->connect_error );
    exit ();
}

$result = $mysqli->query ( $sql );
$array = array();
if ($result) {
    while ( $row = $mysqli->fetch_assoc ( $result ) ) {
        $array[$row['player']] += $row['count'];
    }

    print_r($array); // Return array
}
于 2012-04-26T08:49:12.293 回答