在while循环中分配值,并在循环之外单独访问它们。

时间:2020-12-12 01:38:15

PLATFORM:

平台:

PHP, mySQL

PHP,mySQL

WHAT I HAVE:

我有什么:

I have a Database table. Within my application, I am able to fetch all the rows. When I am querying the database, I have set the records fetch limit dynamically.

我有一个数据库表。在我的应用程序中,我能够获取所有行。当我查询数据库时,我已经动态地设置了记录获取限制。

WHAT I AM TRYING TO DO:

我想做的是:

I am trying to pull out all the rows of data until the record fetch limit is reached, in a loop. I want to assign these results to another array(in the loop) so that I can access these values via this new array, outside the loop. I need the PHP code to do so. I want to be able to apply the same logic of coding in Javascript. Will that be possible?

我正在尝试取出所有的数据行,直到达到记录获取限制,在一个循环中。我想将这些结果分配给另一个数组(在循环中),这样我就可以通过这个新的数组在循环之外访问这些值。我需要PHP代码这样做。我希望能够在Javascript中应用相同的编码逻辑。这是可能的吗?

EXAMPLE:

例子:

//TABLE STRUCTURE
fname   lname    city
Ed       Al      SA
Bob     B       MN
Chris     V       KJ

PHP QUERY:

PHP查询:

$result = mysql_query("SELECT fname, lname, city FROM table LIMIT 3");

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

        $new_rows_data['fname'] .= $row['fname'];
        $new_rows_data['lname'] .= $row['lname'];
        $new_rows_data['city']  .= $row['city'];
    }

DESIRED OUTPUT:

期望的输出:

echo $new_rows_data['fname'][0].'   '.$new_rows_data['lname'][0].'   '.$new_rows_data['city'][0].
//Want the above to show: Ed        Al      SA
echo $new_rows_data['fname'][1].'   '.$new_rows_data['lname'][1].'   '.$new_rows_data['city'][1].
//Want the above to show: Bob       B       MN
echo $new_rows_data['fname'][2].'   '.$new_rows_data['lname'][2].'   '.$new_rows_data['city'][2].
//Want the above to show: Chris     V       KJ

Thank you in advance.

提前谢谢你。

2 个解决方案

#1


3  

If you change the PHP query bits to...

如果您将PHP查询位更改为……

$new_rows_data['fname'][] = $row['fname'];
$new_rows_data['lname'][] = $row['lname'];
$new_rows_data['city'][]  = $row['city'];

...you should be good to go. (At the moment, you're appending the contents into a single string each time.)

…你该走了。(目前,每次都将内容附加到一个字符串中。)

Incidentally, I presume this data is 'known good', as you don't appear to be output escaping it at all. (Cross site scripting is bad, etc.)

顺便说一句,我假定这个数据是“已知的”,因为您似乎根本没有输出来转义它。(跨站点脚本编写很糟糕,等等)

#2


3  

Although middaparka's answer is right, be sure to first declare the individual new arrays before entering the while loop:

尽管middaparka的答案是对的,但请确保在进入while循环之前先声明单个的新数组:

$result = mysql_query("SELECT fname, lname, city FROM table LIMIT 3");

$new_rows_data['fname'] = array();
$new_rows_data['lname'] = array();
$new_rows_data['city'] = array();
while( $row = mysql_fetch_array($result) ) {
    // the following (two brackets []), automatically pushes a value on the end of the array
    $new_rows_data['fname'][] = $row['fname'];
    $new_rows_data['lname'][] = $row['lname'];
    $new_rows_data['city'][]  = $row['city']
}

#1


3  

If you change the PHP query bits to...

如果您将PHP查询位更改为……

$new_rows_data['fname'][] = $row['fname'];
$new_rows_data['lname'][] = $row['lname'];
$new_rows_data['city'][]  = $row['city'];

...you should be good to go. (At the moment, you're appending the contents into a single string each time.)

…你该走了。(目前,每次都将内容附加到一个字符串中。)

Incidentally, I presume this data is 'known good', as you don't appear to be output escaping it at all. (Cross site scripting is bad, etc.)

顺便说一句,我假定这个数据是“已知的”,因为您似乎根本没有输出来转义它。(跨站点脚本编写很糟糕,等等)

#2


3  

Although middaparka's answer is right, be sure to first declare the individual new arrays before entering the while loop:

尽管middaparka的答案是对的,但请确保在进入while循环之前先声明单个的新数组:

$result = mysql_query("SELECT fname, lname, city FROM table LIMIT 3");

$new_rows_data['fname'] = array();
$new_rows_data['lname'] = array();
$new_rows_data['city'] = array();
while( $row = mysql_fetch_array($result) ) {
    // the following (two brackets []), automatically pushes a value on the end of the array
    $new_rows_data['fname'][] = $row['fname'];
    $new_rows_data['lname'][] = $row['lname'];
    $new_rows_data['city'][]  = $row['city']
}