将查询回显到表-将每个列的值打印两次


Echo query to table - prints every column value twice

我刚刚开始PHP开发。我正试图在我们的网络服务器上打印一份每日电话报告。连接成功建立,查询运行,并打印表,但由于某些原因,我似乎无法获得,它在一行中打印每列的值两次。因此,电话代理人John Smith被打印为

John John Smith Smith

表格已格式化。它打印时不会出现错误,但表的宽度是它应该宽度的两倍,因为每个表都打印两次。

此外,我知道mysqli和PDO比我在这里使用的命令更可取。这是我的教科书给出例子的方式——当我对网络开发感到更舒服时,我会切换。现在,即使是简单的事情也让我不知所措。

如果有人能找出问题所在,我将不胜感激。

if (!($connection = mysql_connect($server,$userName,$password))) die("Could not connect");
mysql_select_db($databaseName, $connection);
$myQuery = "Select (a few fields here) 
From 
callreport inner join employees on callreport .`phonenum` = employees.extension
where 
date= 20130807";
if(!($result = mysql_query($myQuery,$connection))) showerror();
while ($row = mysql_fetch_array($result)){
    echo "<tr>";
    foreach ($row as $attribute){
        echo "<td>{$attribute}</td>";
    }
    echo "</tr>";
}
echo "
        </table>
    </body>
</html>";

mysql_fetch_array()以两种方式从表中返回一行-作为数字和关联索引数组:

$row['name']='John';
$row[0]='John';
$row['surname']='Smith';
$row[1]='Smith';

将此函数替换为mysql_fetch_assoc()(仅返回带有关联键的数组)或mysql_fetch_row()(仅返回具有数字键的数组。在这两种情况下,您的脚本都将按照您的预测工作。