注意:数组到字符串的转换 - PHP & mySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16387485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:02:38  来源:igfitidea点击:

Notice: Array to string conversion - PHP & mySQL

phpmysqlphpmyadmin

提问by banana

I've been reading in every thread in here that is related to this but I always get it wrong.

我一直在这里阅读与此相关的每个线程,但我总是弄错。

Please help cause I always get the error

请帮助因为我总是收到错误

"Notice: Array to string conversion" in line "$address[] = mysql_result($row, 0 );"

“注意:数组到字符串的转换”行“$address[] = mysql_result($row, 0);”

below. Please help.

以下。请帮忙。

if ($p_address=mysql_query($email))
{
$address = array();

while($row = mysql_fetch_assoc($p_address))
{     
 $address[] = mysql_result($row, 0 );
}  

$all_address = implode(',', $address);

采纳答案by jerdiggity

Change this line

改变这一行

 $address[] = mysql_result($row, 0 );

To this:

对此:

 $address[] = $row;

And then to see the keys and values available in the new $addressarray, you can do something like this:

然后要查看新$address数组中可用的键和值,您可以执行以下操作:

 print_r($address);

In order to keep implode()functional, do something like this:

为了保持implode()功能,请执行以下操作:

for ($i = 0; $i < count($address); $i++) {
  $all_address[] = implode(',', $address[$i]);
}

Final output:

最终输出:

if ($p_address=mysql_query($email))
{
$address = array();

while($row = mysql_fetch_assoc($p_address))
{     
 $address[] = $row;
}

for ($i = 0; $i < count($address); $i++) {
  $all_address[] = implode(',', $address[$i]);
}

// Example for outputting on screen:
foreach ($all_address as $aa) {
  print $aa . "<br/>\n";
}
}

Hope that helps...

希望有帮助...

回答by Aris

$row is set in every iteration of the while loop. every time it contains a new table record. So you just need to add each record in the address array.

$row 在 while 循环的每次迭代中设置。每次它包含一个新的表记录。所以你只需要在地址数组中添加每条记录。

   while($row = mysql_fetch_assoc($p_address))
   {     
      $address[] = $row;
   }