尝试从foreach循环中的两个表填充两个数组

时间:2013-08-07 20:22:04

标签: php mysql

所有这一切的最终结果应该是应该填充描述和传递的数组,我应该能够回显它们。但是,只有描述会回显但是密码不会回显。从我的所有测试中,对于result1查询,$ value变量看起来是空的。我无法弄清楚如何在第二个查询中使用$ value。

我也遇到了这两个错误: 注意:未定义的变量:在第60行传入/var/www/html/manage/commit.php 警告:在第60行的/var/www/html/manage/commit.php中为foreach()提供的参数无效

// From manage.phones
$result = mysqli_query($con_manage,"SELECT * FROM phones");
while($row = mysqli_fetch_array($result))
{
    $mac[] = $row['mac'];
    $mapped_to[] = $row['mapped_to'];
}

// From asterisk.devices
foreach($mapped_to as $value)
{
  $result = mysqli_query($con_asterisk,"SELECT description as description FROM devices WHERE id='$value'");
  while($row = mysqli_fetch_array($result))
  {
    $description[] = $row['description'];
  }
  $result1 = mysqli_query($con_asterisk,"SELECT data as data FROM sip WHERE id='$value'");
  while($row = mysqli_fetch_array($result))
  {
    $pass[] = $row['data'];
  }

}

// Echo the descriptions and passwds!
echo "Echo all descriptions:";
echo"\n";
foreach($description as $key => $value)
{
  echo "$value\n";
}

echo "Echo all passwds:";
echo"\n";
foreach($pass as $key => $value)
{
  echo "$value\n";
}

3 个答案:

答案 0 :(得分:0)

查询SELECT data as data FROM sip WHERE id='$value'具有空结果集。这就是为什么永远不会执行$pass[] = $row['data']的原因。由于您未在第一个$pass循环之前明确定义$pass = array()(例如foreach),因此在尝试foreach($pass as $key => $value)时会出现两个错误。

答案 1 :(得分:0)

你应该添加

$pass = array();
在foreach循环之前

// From asterisk.devices
foreach($mapped_to as $value) { .... }

答案 2 :(得分:0)

更改

$result1 = mysqli_query($con_asterisk,"SELECT data as data FROM sip WHERE id='$value'");

$result1 = mysqli_query($con_asterisk,"SELECT data as data FROM sip WHERE id='".$value."'");

您应该为两个查询执行此操作。这就是你连接字符串的方式。你的回声同样如此

填充数组,你应该

$description = array(); //initialize your array
while($row = mysqli_fetch_array($result))
{
   array_push($description, $row['description'];
}

为$ pass执行相同操作