将sql查询输出到html表中 [英] Output sql query into html table

查看:111
本文介绍了将sql查询输出到html表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将这个PHP SQL查询的输出放到一个数据库表中,但它将所有的行数据输出到一个列中。

  if(isset($ _ POST ['submit'])){
$ name = htmlentities($ _ POST ['name']);
$ parts = explode(,$ name);
$ lastname = array_pop($ parts);
$ firstname = implode(,$ parts);

$ connection = mysql_connect(localhost,user,password);

mysql_select_db(shoretoshore,$连接);

$ result = mysql_query(SELECT ship_no,shipment_id,arrival_date,origin,destination,lname,fname from shipment,captain WHERE captain.capt_id = shipment.capt_id AND captain.fname ='$ firstname'AND captain.lname ='$ lastname',$ connection);

echo'< table border =0cellpadding =5pxcellspacing =1pxstyle =font-family:Verdana,Geneva,sans-serif; font-size:11px; background -color:#E1E1E1width =100%>
< tr bgcolor =#FFFFFFstyle =font-weight:bold>
< th>货件编号< / th>
货件ID。< / th>
到达日期< / th>
< th> Origin< / th>
< th>目的地< / th>
< th>姓氏< / th>
< th>名字< / th>
< / tr>';
while($ row = mysql_fetch_row($ result)){
foreach($ row as $ value)
print< tr>< td>。{$ value} 。 < / TD>< / TR> 中;
回显< br>;
}
echo< / table>;
}

如何将查询结果输出到HTML表格中?

解决方案

您将 $ value 放在引号内,

尝试:

  while($ row = mysql_fetch_row ($ result)){
echo'< tr>';
foreach($ row为$ value)
{
echo< td>。$ value。< / td>;
}
echo< / tr>;
}


I'm trying to place the output of this PHP SQL query into a database table, but it is outputting all of the row data into one column.

if(isset($_POST['submit'])) {
    $name = htmlentities($_POST['name']);
    $parts = explode(" ", $name);
    $lastname = array_pop($parts);
    $firstname = implode(" ", $parts);

    $connection = mysql_connect("localhost", "user", "password");

    mysql_select_db("shoretoshore", $connection);

    $result = mysql_query("SELECT ship_no, shipment_id, arrival_date, origin,      destination, lname, fname from shipment, captain WHERE captain.capt_id=shipment.capt_id AND captain.fname='$firstname' AND captain.lname='$lastname'", $connection);

    echo '<table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
            <tr bgcolor="#FFFFFF" style="font-weight:bold">
            <th>Shipment No.</th>
            <th>Shipment Id.</th>
            <th>Arrival Date</th>
            <th>Origin</th>
            <th>Destination</th>
            <th>Last Name</th>
            <th>First Name</th>
            </tr>';
    while ($row = mysql_fetch_row($result)) {
        foreach ($row as $value)
            print "<tr><td>"."{$value}"."</td></tr>";
        echo "<br>";
    }
    echo "</table>";
}

How do I output the results of the query into an HTML table?

解决方案

You are putting the $value inside quotation marks, it will be treated as string.

Try:

while ($row = mysql_fetch_row($result)) {
    echo '<tr>';
    foreach ($row as $value)
    {
      echo "<td>".$value."</td>";
    }
    echo "</tr>";
}

这篇关于将sql查询输出到html表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆