Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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 合并两个mysql结果_Php_Mysql_Html Table - Fatal编程技术网

Php 合并两个mysql结果

Php 合并两个mysql结果,php,mysql,html-table,Php,Mysql,Html Table,情况就是这样 while($row = mysql_fetch_assoc($sql)){ $name = $row['name']; $class = $row['class']; $result = $row['phone']; $data .= '<td>'.$name.'</td><td>'.$class.'</td><td>'.$result.'</td>'; } echo "<table>&

情况就是这样

while($row = mysql_fetch_assoc($sql)){

$name = $row['name'];
$class = $row['class'];
$result = $row['phone'];

$data .= '<td>'.$name.'</td><td>'.$class.'</td><td>'.$result.'</td>';
}



echo "<table><tr>'.$data.'</tr><table>"

一种可能的方法是:首先在关联数组中收集结果,按名称*索引,因为它们看起来是唯一的,然后对这些结果执行任何操作。例如:

$results = array();
while ($row = mysql_fetch_assoc($sql)) {
  $name = $row['name'];
  $results[$name]['class'] = $row['class'];
  $results[$name]['phone'][] = $row['phone'];
}

echo '<table>';
foreach ($results as $name => $result) {
  echo '<tr><td>' . $name . '</td><td>' . $result['class'] 
          . '</td><td>' . implode(',', $result['phone']) . '</td></tr>';
}
echo '</table>';
然后,您将有一个给定名称的所有电话已经以逗号分隔的格式提供给您

作为旁注,考虑切换到MySQL或PDO扩展而不是MySQL。这就是它的含义:

从PHP5.5.0开始,此扩展已被弃用,不推荐使用 用于编写新代码,因为它将在将来被删除。相反 应该使用mysqli或PDO_MySQL扩展


这并不像听起来那么简单,但这绝对值得付出努力,仅就准备好的语句而言。

下面将显示一个值以逗号分隔的TD

$result = array();

while($row = mysql_fetch_assoc($sql)){

array_push($result, $row['id']);
}



echo "<table><tr><td>". implode(',',$result) ."</td></tr><table>"

是的,只是我快了一点。关于差异的旁注:首先,我宁愿命名一个变量,该变量用复数名词$result而不是$result来存储项目集合;第二,问题和答案中的引号不匹配。我编辑了问题。“情况有点不同。”尼克更新了答案。作为旁注,你真的应该从一开始就在问题中描述整个情况。相反,你把问题简化得太多了,对问题的原始版本给出的任何答案都没有帮助。。。我没有意识到问题会有所不同。以防万一,如果我有更多的行只有一个值,而不是名称和类,我应该做同样的事情吗?@nick好的,我会遵循经验法则-如果只有一个值,将其存储为类似['class']的基元,如果可以有多个,请选择一个数组。
  SELECT name, class, CONCAT_WS(',', phone) AS phones
    FROM students
GROUP BY name 
$result = array();

while($row = mysql_fetch_assoc($sql)){

array_push($result, $row['id']);
}



echo "<table><tr><td>". implode(',',$result) ."</td></tr><table>"