Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为while循环结果创建一个变量_Php_Mysql_Arrays_While Loop_Echo - Fatal编程技术网

Php 为while循环结果创建一个变量

Php 为while循环结果创建一个变量,php,mysql,arrays,while-loop,echo,Php,Mysql,Arrays,While Loop,Echo,我的while循环的结果如下所示: 苹果橙香蕉 我希望能够获取所有这些结果,并将它们放入一个变量中,使其看起来像这样: $水果=苹果橙香蕉 我怎样才能做到这一点?浓缩操作员= while ($topic = mysql_fetch_assoc ($result)); { echo "{$topic["overtag "]} "; } 浓缩操作员= while ($topic = mysql_fetch_assoc ($result)); { echo "{$topic["overt

我的while循环的结果如下所示: 苹果橙香蕉

我希望能够获取所有这些结果,并将它们放入一个变量中,使其看起来像这样: $水果=苹果橙香蕉


我怎样才能做到这一点?

浓缩操作员=

while ($topic = mysql_fetch_assoc ($result)); {
   echo "{$topic["overtag "]} ";
}

浓缩操作员=

while ($topic = mysql_fetch_assoc ($result)); {
   echo "{$topic["overtag "]} ";
}

您只需要将每个变量连接到循环内的变量上

$fruits = '';
while ($topic = mysql_fetch_assoc ($result)); {
    $fruits .= "{$topic["overtag "]} ";
}
哦,还有,你有一个错误的分号,它将打破你的while循环:

$fruits = "";
while ($topic = mysql_fetch_assoc ($result)); {
  echo "{$topic["overtag "]} ";
  $fruits .= $topic['overtag'] . " ";
}
// This is going to result in an extra space at the end, so:
$fruits = trim($fruits);
应该是:

while ($topic = mysql_fetch_assoc ($result)); {
                                   --------^^^--

您只需要将每个变量连接到循环内的变量上

$fruits = '';
while ($topic = mysql_fetch_assoc ($result)); {
    $fruits .= "{$topic["overtag "]} ";
}
哦,还有,你有一个错误的分号,它将打破你的while循环:

$fruits = "";
while ($topic = mysql_fetch_assoc ($result)); {
  echo "{$topic["overtag "]} ";
  $fruits .= $topic['overtag'] . " ";
}
// This is going to result in an extra space at the end, so:
$fruits = trim($fruits);
应该是:

while ($topic = mysql_fetch_assoc ($result)); {
                                   --------^^^--

使用下面的PHP代码,您可以从数据库表中获取数据并显示在网页上:

// I love arrays.
$fruits = array();
while ($topic = mysql_fetch_assoc ($result)); {
   $fruits[] = (string)$topic["overtag "];
}

// If you don't want an array, but a string instead, use implode:

$fruits = implode(' ', $fruits)

使用下面的PHP代码,您可以从数据库表中获取数据并显示在网页上:

// I love arrays.
$fruits = array();
while ($topic = mysql_fetch_assoc ($result)); {
   $fruits[] = (string)$topic["overtag "];
}

// If you don't want an array, but a string instead, use implode:

$fruits = implode(' ', $fruits)

谢谢GolezTrol,但是当我这样做时,结果只是显示“Array”,我的实际数组没有出现。我在第[]3行去掉了$fruits后面的[]括号,这给了我想要的结果,但只有当我在while循环中回响时。当我在while循环之外回显$fruits时,我只得到一个结果,这意味着我只得到“apples”而不是“apples orange banana”。如果愿意,可以使用
内爆
,如循环下的代码行所示。当然,您可以只连接字符串,但在实践中,在许多情况下,您需要使用从数据库检索的独立值,这就是我将它们放入数组的原因。数组是保存数据列表的非常强大的工具。如果要将数组中的值转换为单个字符串,可以使用
内爆
。感谢GolezTrol,但当我这样做时,结果只是显示“array”,我的实际数组不会出现。我去掉了第[]3行$fruits后面的[]方括号,这就得到了我想要的结果,但只有当我在while循环中回响时。当我在while循环之外回显$fruits时,我只得到一个结果,这意味着我只得到“apples”而不是“apples orange banana”。如果愿意,可以使用
内爆
,如循环下的代码行所示。当然,您可以只连接字符串,但在实践中,在许多情况下,您需要使用从数据库检索的独立值,这就是我将它们放入数组的原因。数组是保存数据列表的非常强大的工具。如果您想将数组中的值转换为单个字符串,可以使用
内爆
。欢迎使用堆栈溢出!虽然这个代码片段可能是解决方案,但它确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。欢迎使用堆栈溢出!虽然这个代码片段可能是解决方案,但它确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您代码建议的原因。这是我的首选$result。=$rows['name'];这是我的首选项$result.=$rows['name'];