-1

我像这样从 mytable 查询。

在此处输入图像描述

我想把它放在数组(PHP)中并像这样搜索

$index = 15;
$result = array_search(...)..);   <--- age_range

并将该行($result)放入我创建的新数组中;或者有人有更好的主意。对不起一些愚蠢的问题。感谢您的帮助。

询问

SELECT 
  CONCAT(2 * FLOOR(age / 2), '-', 2 * FLOOR(age / 2) + 5) AS 'age_range',
  SUM(gload1) AS 'g1',
  SUM(gload2) AS 'g2',
  SUM(gload3) AS 'g3',
  SUM(gload4) AS 'g4',
  SUM(gload5) AS 'g5',
  SUM(gload6) AS 'g6',
  SUM(gload7) AS 'g7',
  SUM(gload8) AS 'g8',
  SUM(gload9) AS 'g9',
  SUM(gload10) AS 'g10' 
FROM
  member 
GROUP BY 1 
ORDER BY age ;
4

2 回答 2

1

你有两个选择:

选项一 - 从表中选择所有行,然后在 PHP 级别搜索:

$result = mysqli_query($link,"SELECT * FROM `mytable`;");
$arrayForAllRows = array();
while ($row = mysqli_fetch_assoc($result)) {
    array_push($arrayForAllRows,$row);
}
echo array_search("34-36",$arrayForAllRows);

(未测试)

选项二 - 首先获取正确的数据(mysql):

$result = mysqli_query($link,"SELECT * FROM `mytable` WHERE `age_range`='34-36'");
while ($row = mysqli_fetch_assoc($result)) {
    // Do somthing with each row..
}
于 2013-09-27T21:11:32.050 回答
1

如果您确实需要所有可用的信息,则可以在迭代时将其放入可读的关联数组中。

$result = mysqli_query( "SELECT.. " ); 

$x = 1;
while( $row = mysqli_fetch_array( $result )) {
    $Label = "g". $x ."";
    $Ages[$row["age_range"]] = array( $Label => $row["g". $x++ .""] );
}

print_r( $Ages );

/**
 * Expected Result:
**/
Array
(
[0] => Array
    (
        [14-16] => Array 
        (
             [g1] => 3102
             [g2] => 5305
             [g3] => 4104
             [g4] => 1909
             [g5] => 7428
             [g6] => 12500
        )
    )

 //Various array functions to search for a value,
 //in_array, array_search etc.

但最好只检索你需要的东西,尽管情况并非总是如此。

于 2013-09-27T21:12:30.220 回答