显示每个表数据中的列数据

时间:2013-06-13 06:23:33

标签: php html sql sql-server

---------------------------------------------------------------------
Quantity  |   Unit  |  Detials of Items |  ISSUED DATE  |  RETURNED |
          |         |                   |               |           |
---------------------------------------------------------------------
tbl col 1 |tbl col2 | tbl col2          |  tbl col 3    | tbl col4  |
---------------------------------------------------------------------

说我在数据库中有一个包含4列的表

这就是我网站上的表格

如何在每个表数据中显示我的表列数据?

请提出任何建议

我只有这个代码,因为我刚接触php我只是使用macromedia作为接口

$myServer = "server";
 $myUser = "user";
 $myPass = "password";
 $myDB = "mssqldb"; 

 //connection to the database
 $dbhandle = mssql_connect($myServer, $myUser, $myPass)
     or die("Couldn't connect to SQL Server on $myServer"); 

 //select a database to work with
      $selected = mssql_select_db($myDB, $dbhandle)
          or die("Couldn't open database $myDB"); 

 //declare the SQL statement that will query the database
     $query = "SELECT eidnumber ";
     $query .= "FROM tablename ";

<table width="935" height="102" border="1">
      <tr>
        <th width="89" rowspan="2" scope="col">Quantity</th>
        <th width="87" rowspan="2" scope="col">Unit</th>
        <th width="137" rowspan="2" scope="col">Details of Item(s) Accounted </th>
        <th width="221" rowspan="2" scope="col">ISSUED DATE </th>
        <th height="51" colspan="3" scope="col">RETURNED</th>
        <th width="254" rowspan="2" scope="col">
    <p>REMARKS</p>    </th>
      </tr>
      <tr>
        <th width="24" height="39" scope="col">C</th>
        <th width="18" scope="col">X</th>
        <th width="53" scope="col">DATE</th>
      </tr>
    </table>
    <table width="935" border="1">
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
      <tr>
        <td width="90">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="137">&nbsp;</td>
        <td width="221">&nbsp;</td>
        <td width="24">&nbsp;</td>
        <td width="17">&nbsp;</td>
        <td width="55">&nbsp;</td>
        <td width="253">&nbsp;</td>
      </tr>
    </table>

2 个答案:

答案 0 :(得分:5)

如果您使用PDO,这是一种更好的方法:

echo "<table>";
$sql = $this->db->query("SELECT * FROM YOUR_TABLE");
while($row = $sql->fetch(PDO::FETCH_ASSOC))
{
    echo "
    <tr>
        <td>". $row['quantity'] ."</td>
        <td>". $row['unit'] ."</td>
        <td>". $row['details'] ."</td>
        <td>". $row['issued_date'] ."</td>
        <td>". $row['returned'] ."</td>
    </tr>
    ";
}
echo "</table>";

或者,如果您是mysql_函数,则弃用:

echo "<table>";
$sql = mysql_query("SELECT * FROM YOUR_TABLE");
while($row = mysql_fetch_assoc($sql))
{
    echo "
    <tr>
        <td>". $row['quantity'] ."</td>
        <td>". $row['unit'] ."</td>
        <td>". $row['details'] ."</td>
        <td>". $row['issued_date'] ."</td>
        <td>". $row['returned'] ."</td>
    </tr>
    ";
}
echo "/<table>";

答案 1 :(得分:3)

除了上面的答案,你还可以使用mysqli。这是一个例子:

$result = $mysqli->query($query);
while ($row = $result->fetch_object()){
    echo "<tr>";
    echo "<td>" . $row->quantity . "</td>"
...etc...
}