为二维数组赋值

发布于 2024-12-02 05:56:43 字数 349 浏览 0 评论 0原文

我试图将数据库中的值分配给二维数组,但它只显示最后一项。

这是代码:

while($row = mysql_fetch_array($results)){
$MyData = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While

我做错了什么请帮忙。

I am trying to assign values from a db to a 2d array, but its only showing the last iterms.

Here is the code:

while($row = mysql_fetch_array($results)){
$MyData = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While

What am I doing wrong please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囍笑 2024-12-09 05:56:43
$MyData[] = $row;

我还建议创建一个函数就足够了

,因为从数据库获取数组是一个非常常见的例程。
因此,您将能够在一行中获取数据,

$myData = getRows("SELECT * FROM table");
$MyData[] = $row;

would be enough

I'd also suggest to make a function, as getting an array from db is a very common routine.
So, you'll be able to get your data in one line,

$myData = getRows("SELECT * FROM table");
瑶笙 2024-12-09 05:56:43
$myData = array();
while($row = mysql_fetch_array($results)){
$MyData[] = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While

这就能解决问题

$myData = array();
while($row = mysql_fetch_array($results)){
$MyData[] = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While

this will do the trick

久隐师 2024-12-09 05:56:43

每次循环运行时都会声明一个新数组。将其声明为 while 循环之外,并添加新值。

$MyData = array();
while($row = mysql_fetch_array($results)){
$MyData[] = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While

You're declaring a new array each time the loop runs. Declare it out of the while loop, and add the new values.

$MyData = array();
while($row = mysql_fetch_array($results)){
$MyData[] = array( array("Focus Area", $row["FocusArea"]),
               array("Finding Title", $row["FindingTitle"]),
               array("Finding Detail", $row["FindindDetail"]) 
             ); 

}//End While
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文