【问题标题】:How to display all data from a table?如何显示表格中的所有数据?
【发布时间】:2012-02-24 19:53:12
【问题描述】:

好吧,我一直在搜索谷歌,但我仍然无法找到如何做到这一点。

我是 php 的初学者,所以我真的很难过。

无论如何,我需要做的是从表格中获取所有数据并将其显示在我的页面上。

喜欢

第 1 行的内容

第 2 行的内容

等等

很好,因为寻求帮助而投了反对票。

【问题讨论】:

  • 你在开玩笑,你没有得到thisthis 的一个例子吗??

标签: php mysql html web


【解决方案1】:

这可能对你有帮助

print_r() 以人类可读的方式显示有关变量的信息。

print_r()var_dump()var_export() 还将显示 PHP 5 对象的受保护和私有属性。不会显示静态类成员。

请记住,print_r() 会将数组指针移动到末尾。使用reset() 将其带回到开头。

http://php.net/manual/en/function.print-r.php

【讨论】:

    【解决方案2】:

    这几乎是 PHP DB 访问 101

    $pdo = new PDO('mysql:host=localhost;dbname=myDbName', 'username', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt = $pdo->prepare('SELECT * FROM a_table');
    $stmt->execute();
    
    $resultSet = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($resultSet as $idx => $row) {
        echo '<p>Contents of row ', $idx + 1, '</p><dl>';
        foreach ($row as $col => $val) {
            printf('<dt>%s</dt><dd>%s</dd>',
                   htmlspecialchars($col),
                   htmlspecialchars($val));
        }
        echo '</dl>';
    }
    

    【讨论】:

      【解决方案3】:

      我认为谷歌应该给你答案,因为这是一个相当容易回答的问题,但是当你开始时,你并不总是知道要搜索什么。

      无论如何,希望这会有所帮助。

      <?php
      // connect with you database, returns boolean so you know if you succeeded or not
      $con = mysql_connect($database,$username,$password);
      if(!$scon){
          die('Could not connect to database'); // Stop execution if connection fails
          }
      
      //create your query
      $query = "Place your database query here";
      
      //get the results
      $result = mysql_query($query);
      
      //now you want to go through each row of the result table and echo the contents, or
      //use them for whatever reason
      while($row = mysql_fetch_array($result)){
          echo $row['field_you_want_to_display'];
          echo $row['another_field_you_want_to_display']; //You see where this is going
          }
      
      //After doing what you want, close the connection to the database
      mysql_close($con);
      ?>
      

      另外,您可能想看看documentation of php,了解您以前从未见过的功能。

      【讨论】:

        猜你喜欢
        • 2012-05-15
        • 2012-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-26
        • 1970-01-01
        • 2016-02-22
        • 2020-12-19
        相关资源
        最近更新 更多