【问题标题】:Could someone explain this php mysql script? [closed]有人可以解释这个 php mysql 脚本吗? [关闭]
【发布时间】:2013-02-27 13:34:07
【问题描述】:

有人能解释一下这个脚本执行时到底发生了什么吗?它就像一个魅力,但我不知道它到底是做什么的。

基本上我有一个带有以下字段的 sql 表 [images]

  • 文件名
  • 关心
  • 网址

我想要做的是获取 [URL] 的所有值并将其放入一个数组中

为了实现这一点,我习惯于遵循我在 stackoverflow 上找到的脚本

$images = mysql_query("Select URL from images where Category = 'Management' ");
$imagerow = Array();

while ( $row = mysql_fetch_assoc($images) )
{
$imagerow[] = $row['URL'];
}

echo $imagerow[0] ;

该脚本的工作原理很吸引人,但是对于 php 和 mysql 来说,我发现很难理解到底发生了什么。如果有人用简单的话解释会发生什么,我会很高兴的。

【问题讨论】:

  • 查询结果存储在$images中,迭代完成。 'URL' 列从单个记录中提取并存储到新的占位符 $imagerow 中,然后根据需要使用。
  • 我不明白为什么要使用while 循环。 @ranty 的回答虽然解决了它。我很抱歉问了一个不必要的问题,只是我不相信复制粘贴代码。
  • @TDsouza:没关系。请确保您下次清楚地解释您不理解的内容。

标签: php mysql sql database arrays


【解决方案1】:

这个问题不属于这里,但我会回答这个问题,以便在不打扰版主的情况下结束问题。

// mysql query is executed
$images = mysql_query("Select URL from images where Category = 'Management' ");

// empty array initialized
$imagerow = Array();

// while there are results of the executed mysql query
while ( $row = mysql_fetch_assoc($images) )
{
    // create a new element in $imagerow array and put url of the image there
    $imagerow[] = $row['URL'];
}

// output first element of the array we have just filled
echo $imagerow[0] ;

【讨论】:

  • 谢谢你把事情说清楚了。让我困惑的是while
猜你喜欢
  • 2015-10-28
  • 2014-10-13
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
相关资源
最近更新 更多