使用PHP和SQL基于表值填充数组

使用PHP和SQL基于表值填充数组,php,sql,arrays,database,Php,Sql,Arrays,Database,我想知道如何用从SQL数据库检索的数据填充数组并将其输出到HTML表中 我使用下面的表结构 CREATE TABLE PROJECT_RECORDS ( RECORDS_ROWID number(3) not null, RECORDS_LISTCOLUMNID number (3) not null, RECORDS_RECORDVALUE varchar2 (25), constraint PROJECT_RECORDS primary key(RECORDS_ROWID, RECORDS_

我想知道如何用从SQL数据库检索的数据填充数组并将其输出到HTML表中

我使用下面的表结构

CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
constraint PROJECT_RECORDS primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID));
样本数据

RECORDS_ROWID RECORDS_LISTCOLUMNID RECORDS_RECORDVALUE     
------------- -------------------- --------------------
        1                    1     Sample                    
        1                    2     Sample description          
        2                    1     Data                      
        2                    2     Data Description          
从这里我想填充一个html表,例如

|Sample | Sample description |
|Data   | Data Description   |
我目前只能选择数据并将其放入一个数组,将所有记录\u RECORDVALUE放入一个表列中,这不是我想要的


我相信您会使用循环为每个记录\u ROWID填充一个数组,然后将其回显到一个表中。如有任何帮助,将不胜感激。

将echo标记中的代码替换为字段名

<table border="1" width="100%" cellspacing="0" cellpadding="0">
    <tr>
        <td>Sample</td>
        <td>Sample Description</td>
        <td>Data</td>
        <td>Data Description</td>
    </tr>
<?php
$stmt = mysql_query("SELECT * from PROJECT_RECORDS");
while ($data = mysql_fetch_array($stmt))
{
    <tr>
        <td><?php echo $data['Sample'];?></td>
        <td><?php echo $data['Sample_Description'];?></td>
        <td><?php echo $data['Data'];?></td>
        <td><?php echo $data['Data_Description'];?></td>
    </tr>
    <?php } ?>
</table>

样品
样本描述
资料
数据描述

首先,您需要连接到数据库,因此:

<?php
$con=mysqli_connect("localhost","your username","your password","your database");

if (mysqli_connect_errno())
  {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
这将查询您的数据库。现在,以表格形式回显您的查询:

 echo "<table border='1' cellpadding='10'>";
    echo "<tr> <th>Sample</th> <th>Sample Description</th></tr>";

 while($row = mysqli_fetch_array($result))
 {
  echo '<td>' . $row['name of your desired outputted column'] . '</td>';
  echo '<td>' . $row['name of your  other desired outputted column'] . '</td>';
  echo "</tr>"; 
  }
 echo "</table>";
  ?>
echo”“;
echo“样本描述”;
while($row=mysqli\u fetch\u数组($result))
{
回显'.$row['所需输出列的名称'].';
回显'.$row['其他所需输出列的名称'].';
回声“;
}
回声“;
?>

这应该行得通。现在我刚刚创建了一个两列的表,但是我假设您知道足够多的html来创建您想要的表

以下代码可以解决您的问题

<?php
$link=mysqli_connect("host","username","password","database") or die("Error " . mysqli_error($link));

$result = mysqli_query($link,"SELECT * FROM PROJECT_RECORDS");

$counter = 1;

echo "<table border='1'>";
    echo "<tr> <th>Serial No.</th><th>Row ID</th> <th>Column ID</th> <th>Sample Description</th></tr>";

while($row = mysqli_fetch_assoc($result)) {
  echo '<td>' . $counter .'</td>'.
  echo '<td>' . $row['RECORDS_ROWID'] . '</td>';
  echo '<td>' . $row['RECORDS_LISTCOLUMNID'] . '</td>';
  echo '<td>' . $row['RECORDS_RECORDVALUE'] . '</td>';

  echo "</tr>"; 
}
 echo "</table>";
  ?>


您可以使用正在使用的特定DBMS进行标记,并删除推测性实现细节的标记(foreach、while循环),从而获得更有用的答案。由于您正在请求实现建议,最好不要将该建议与实现相关的标记相混淆。虽然它是此建议的副本,但我建议将此建议视为副本,因为后者包含您的尝试。请不要多次提问,因为这会产生不必要的工作。在
htmlentities
中包装字符串输出可能是值得的,否则可能会引入XSS漏洞。此外,最好允许静态HTML存储为HTML,而不是将其包装在echo标记中,这样IDE就可以理解文档的结构。在
htmlentities
中包装字符串输出可能是值得的,否则可能会引入XSS漏洞+但是,对于没有在echo语句中包装所有HTML,我感到非常抱歉!在
htmlentities
中包装字符串输出可能是值得的,否则可能会引入XSS漏洞。另外,最好允许静态HTML存储为HTML,而不是将其包装在echo标记中,这样IDE就可以理解文档的结构。
<?php
$link=mysqli_connect("host","username","password","database") or die("Error " . mysqli_error($link));

$result = mysqli_query($link,"SELECT * FROM PROJECT_RECORDS");

$counter = 1;

echo "<table border='1'>";
    echo "<tr> <th>Serial No.</th><th>Row ID</th> <th>Column ID</th> <th>Sample Description</th></tr>";

while($row = mysqli_fetch_assoc($result)) {
  echo '<td>' . $counter .'</td>'.
  echo '<td>' . $row['RECORDS_ROWID'] . '</td>';
  echo '<td>' . $row['RECORDS_LISTCOLUMNID'] . '</td>';
  echo '<td>' . $row['RECORDS_RECORDVALUE'] . '</td>';

  echo "</tr>"; 
}
 echo "</table>";
  ?>