【问题标题】:PHP pdo echo results into tablePHP pdo 将结果回显到表中
【发布时间】:2016-06-22 10:44:13
【问题描述】:

我有一个简单的SELECT 函数,我想将结果回显到一个表格中。

到目前为止,我能够以线性方式显示结果,但不能以表格形式显示结果。

每当我echo <tr>(参见下面的代码)时,它似乎都被忽略了。 我做错了什么?

到目前为止我的代码是;

function.php

class crud {
    public function lastLogin() {
        $stmt = $this->db->prepare("SELECT * FROM logins WHERE userId=:userId");
        $stmt->bindparam(":userId", $_SESSION['user_session']);
        $stmt->execute();

        if ($stmt->rowCount() > 0) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo '<tr>'.$row['lastLogin'].'</tr>'; // <- this line 
            }
        }
        else {
            echo 'Nothing here';
        }
        return true;
    }
}

user.php

<div class="col-lg-6">
    <?php $crud->lastLogin(); ?>
</div>

只是为了确认一下,使用上面的代码确实显示了数据 - 只是不像我想要的那样在表格中。以如下格式显示;

2016-03-07 17:09:382016-03-08 09:12:082016-03-08 09:13:342016-03-08 09:15:022016-03-08 09:15:342016-03-08 11:42:33

我确定我错过了一些简单的东西。任何帮助表示赞赏。

【问题讨论】:

  • “每当我添加 、 或
    时,它们似乎都被忽略了。我做错了什么?” - 向我们展示你的表现那就再用吧。
  • database connection 在你的课堂上不见了!!
  • 我不确定 bindParam 是否等于 pdo 中的 bindparam
  • return true 形成你的功能,这就是你没有回显的原因。相反,您需要返回 $row['lastLogin']
  • 这是一个表格语法示例&lt;table border="1" width="100%"&gt; &lt;tr&gt; &lt;td width="100%"&gt;ABC&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="100%"&gt;123&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;

标签: php html mysql function pdo


【解决方案1】:

如cmets中所述:

这是一个表格语法示例

<table border="1" width="100%"> 
   <tr> 
<td width="100%">ABC</td> 
   </tr> <tr> 
<td width="100%">123</td> 
   </tr> 
</table>

你最终使用的是什么:

echo '<table><tr><td>'.$row['lastLogin'].'</td></tr></table>';

【讨论】:

    猜你喜欢
    • 2012-11-21
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 2013-06-26
    • 2016-05-22
    • 1970-01-01
    • 2021-08-14
    • 2013-07-31
    相关资源
    最近更新 更多