1

我正在编写一个 php 脚本来从 MYSQL 数据库中提取一些数据,然后我想获取这些数据并将其存储到一个二维数组中。正常考虑,如果我使用这样的代码

$test = array();
$test[0]=array();
$test[0]['hello']='hello';
$test[1]=array();
$test[1]['hello']='hello';
$test[2]=array();
$test[2]['hello']='hello';
print_r($test);

输出将是:

Array ( [0] => Array ( [hello] => hello ) [1] => Array ( [hello] => hello ) [2] =>
Array ( [hello] => hello ) )

这就是我希望我的输出的方式

所以这就是我在脚本中所做的

所以基本上在我的数据库中,我有一个女子联赛排名表,列是

team_name, played, won, drawn, lost, for, against, points

所有连接都已成功处理,以下是我的查询

$get_ladies_query = "SELECT 
`team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_ladies` order by pos";

在我展示下一个代码之前,重要的一点是,还有 2 个其他排名表 men_senior 和 men_intermediate 具有相同的结构,但显然只有数据发生变化,下面是两个查询,以防万一

$get_mens_inter_query = "SELECT 
`team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_inter` order by pos";

    $get_mens_senior_query = "SELECT 
`team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_senior` order by pos";

现在我创建了 3 个数组,我想分别为女士、男士高级、男士中级保持排名

$standings_ladies = array();
$standings_men_inter = array();
$standings_men_senior = array();

我想在数组中显示的数据是这样的

array(0=>array(team_name,wins,drawn,lost,for,against,points)
1=>array(team_name,wins,drawn,lost,for,against,points)) and so on

现在,由于我想为所有 3 个类别创建排名的多维数组,我可以在 3 个单独的 while 循环中运行查询,尽管我认为,我可以在 1 中完成相同的结果,并且我认为这将有助于提高性能。如果使用 3 个 while 循环更好,请告诉我,我尝试过的内容如下。

//I run the 3 queries and store them in the given variables
$result_mens_senior = mysqli_query($link,$get_mens_senior_query);
$result_mens_inter = mysqli_query($link,$get_mens_inter_query);
$result_ladies= mysqli_query($link, $get_ladies_query);

//I want to create 1 while loop so based of the results returned from the 3 
//queries so based on the results returned from the 3 queries, 
//I get the max number of times I want the query to run
$ladies_boundary = mysqli_num_rows($result_ladies);
$mens_senior_boundary = mysqli_num_rows($result_mens_senior);
$mens_inter_boundary = mysqli_num_rows($result_mens_inter);
$max_size = max(array($ladies_boundary,$mens_senior_boundary,$mens_inter_boundary));

//set an index to start from 0
$index = 0;

//I will only show the example for 1 of the arrays but you get an idea
that this issue will occur for all
while ($index < $max_size)
{
//first, everytime the loop is entered, we need the next row to be fetched

    $standings_men_inter_table = mysqli_fetch_assoc($result_mens_inter);
    $standings_ladies_table = mysqli_fetch_assoc($result_ladies);

//there is a high chance that the other two tables return a different row size
//so its best to check that we do not go beyond
if($index < $mens_senior_boundary)
    {
            //we fetch the rows every time we enter the block
            $standings_men_senior_table = mysqli_fetch_assoc($result_mens_senior);

            //then, this is how I attempt at creating the 2 dimensional array
        array_push($standings_men_senior, array(
        $standings_men_senior_table['team_name'],
        $standings_men_senior_table['played'],
        $standings_men_senior_table['won'],
        $standings_men_senior_table['drawn'],
        $standings_men_senior_table['lost'],
        $standings_men_senior_table['for'],
        $standings_men_senior_table['against'],
        $standings_men_senior_table['points']));
    }

//incrementing index each time the loop runs
$index++;

}

最后,我只想打印我认为是数组的东西,但得到这个,附上图片,希望你能看清楚 在此处输入图像描述

只是为了进一步调查,每次输入“if”块时,我都将所有内容都注释掉,然后将其放入以查看返回的内容

if($index < $mens_senior_boundary)
{
    print_r(mysqli_fetch_assoc($result_mens_senior));
}

我得到的输出几乎是我需要的 90%

Array 
([team_name] => Morley Gaels [played] => 8 [won] => 6 [drawn] => 2 
[lost] => 0 [for] => 110 [against] => 83 [points] => 14 ) 
Array ( [team_name] => Southern Districts [played] => 8 [won] => 3 [drawn] => 2 
[lost] => 3 [for] => 104 [against] => 98 [points] => 8 ) 
Array ( [team_name] => St Finbarrs [played] => 8 [won] => 3 [drawn] => 2 
[lost] => 3 [for] => 107 [against] => 99 [points] => 8 ) 
Array ( [team_name] => Western Shamrocks [played] => 8 [won] => 3 [drawn] => 0 
[lost] => 5 [for] => 96 [against] => 88 [points] => 6 ) 
Array ( [team_name] => Greenwood [played] => 8 [won] => 1 [drawn] => 1 
[lost] => 9 [for] => 82 [against] => 109 [points] => 3 )

我需要的是例如:

Array(0=>Array 
([team_name] => Morley Gaels [played] => 8 [won] => 6 [drawn] => 2 
[lost] => 0 [for] => 110 [against] => 83 [points] => 14 )
1=>Array
([team_name] => Southern Districts [played] => 8 [won] => 3 [drawn] => 2 
[lost] => 3 [for] => 104 [against] => 98 [points] => 8 )..... so on);

我的问题是

  • 我的代码有什么问题,在 php 中动态创建多维数组的正确方法是什么?
    • 关于 mysql_fetch_assoc 的工作原理和返回方式,我有什么不明白的吗?
    • 有什么需要改进的地方,我做错了什么吗?

感谢您的时间,而不是您阅读它,我试图尽可能详细地了解我的尝试。

谢谢你。

4

2 回答 2

1

不要试图将所有内容放在一个循环中,它会显着降低您的代码清晰度,并且几乎不会给您带来任何性能提升,对此有一个术语,称为微优化,除非它确实有必要(大型站点上的瓶颈)。

现在,我向您建议的是重新制作您的循环,并尽可能清楚地说明如何操作数据。

在您的情况下,最重要的事情是在流程的每个步骤中调试、打印数组的内容,以检查它包含的内容,从数据库到代码末尾,您会发现问题出在哪里。

于 2013-07-17T19:17:23.647 回答
1

尝试这个

执行此操作后:

$result_mens_senior = mysqli_query($link,$get_mens_senior_query);
$result_mens_inter = mysqli_query($link,$get_mens_inter_query);
$result_ladies= mysqli_query($link, $get_ladies_query);

就这样做

while ($standings_men_senior[] = mysqli_fetch_assoc($result_mens_senior)){}
while ($standings_men_inter[] = mysqli_fetch_assoc($result_mens_inter)){}
while ($standings_ladies[] = mysqli_fetch_assoc($result_ladies)){}

基本上所有发布的代码都应该可以替换为:

<?php
$ladies_query = "SELECT `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_ladies` order by pos";

$inter_query = "SELECT `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_inter` order by pos";

$senior_query = "SELECT `team_name`, `played`, `won`, `drawn`, `lost`, `for`, `against`, `points` 
FROM `standings_men_senior` order by pos";

$ladies_stmt = mysqli_query($link, $ladies_query) || die ("Couldn't get Ladies"); // reminds me of high school
$inter_stmt  = mysqli_query($link, $inter_query)  || die ("Couldn't get Inter");
$senior_stmt = mysqli_query($link, $serior_query) || die ("Couldn't get Seniors");

$standings_men_senior = array();
$standings_men_inter = array();
$standings_ladies = array();

while ($row = mysqli_fetch_assoc($senior_stmt)){
    $standings_men_senior[] = $row;
}
while ($row = mysqli_fetch_assoc($inter_stmt)){
    $standings_men_inter[] = $row;
}
while ($row = mysqli_fetch_assoc($ladies_stmt)){
    $standings_ladies[] = $row;
}
于 2013-07-17T19:17:27.163 回答