如何在PHP中输出MySQL表

发布于 2025-01-01 03:48:59 字数 356 浏览 4 评论 0原文

我正在测试这段代码

$query = "SELECT * from `items` where category_id=?";
$stmt = $mysqli->query($query);
$stmt->bind_param("s", $category_id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $stmt->fetch_assoc()) {
    printf ("%s (%s)\n", $row['name'], $row['code']);
}

有没有更简单的方法将整个表输出到 HTML 标记?

I'm testing this code

$query = "SELECT * from `items` where category_id=?";
$stmt = $mysqli->query($query);
$stmt->bind_param("s", $category_id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $stmt->fetch_assoc()) {
    printf ("%s (%s)\n", $row['name'], $row['code']);
}

Is there any easier way to output whole table to HTML markup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼藏柔 2025-01-08 03:48:59

是否有更简单的方法将整个表格输出为 HTML 标记?

当然。
学习使用模板

只需将准备好的语句与输出分开即可。
为此,首先将数据收集到 array:

/* fetch values */
$data = array();
while ($row = $stmt->fetch_assoc()) {
    $data[] = $row;
}

中,然后使用您喜欢的任何 HTML 标记开始输出(最好通过包含单独的模板文件:

<table>
<?php foreach($data as $row): ?>
  <tr>
    <td>
      <a href="?id=<?= htmlspecialchars($row['id']) ?>">
        <?= htmlspecialchars($row['name']) ?>
      </a>
    </td>
  </tr>
<?php endforeach ?>
</table>
  

Is there any easier way to output whole table to HTML markup?

Sure.
Learn to use templates.

Just separate your prepared statements from output.
To do so, at first collect your data into array:

/* fetch values */
$data = array();
while ($row = $stmt->fetch_assoc()) {
    $data[] = $row;
}

and then start output with whatever HTML markup you like (preferably by means of including a separate template file:

<table>
<?php foreach($data as $row): ?>
  <tr>
    <td>
      <a href="?id=<?= htmlspecialchars($row['id']) ?>">
        <?= htmlspecialchars($row['name']) ?>
      </a>
    </td>
  </tr>
<?php endforeach ?>
</table>
  
傲娇萝莉攻 2025-01-08 03:48:59

以下代码片段可以满足您的要求..但我不确定这是否是更简单的方法..希望这会有所帮助。

对于 php,

$pricequery="SELECT price FROM technoxchange";
$result=mysql_query($pricequery);

while($row= mysql_fetch_array($result)){
   $prices [] = $row['price'];
}

echo json_encode( array( 'prices' => $prices ) ); 

对于 js,

var p;
$.get("getTechnoXchange.php", function(data){
    p = data.prices;
});

$('#priceUnicus').html( p[0] ); 
$('#priceHire').html( p[1] ); 
$('#priceMonsterArena').html( p[2] ); 

The following code snippet can match your requirements..but i'm not sure whether this is easier way or not..Hope this will help.

For the php,

$pricequery="SELECT price FROM technoxchange";
$result=mysql_query($pricequery);

while($row= mysql_fetch_array($result)){
   $prices [] = $row['price'];
}

echo json_encode( array( 'prices' => $prices ) ); 

For the js,

var p;
$.get("getTechnoXchange.php", function(data){
    p = data.prices;
});

$('#priceUnicus').html( p[0] ); 
$('#priceHire').html( p[1] ); 
$('#priceMonsterArena').html( p[2] ); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文