使用MySQL表中的数据动态填充div

时间:2015-10-10 21:04:32

标签: php html mysql sql

假设我有一张人员表:

forename    surname    age    gender
--------------------------------------------
adam        example    90      male
john        example    90      male

如果我想在不同的div中显示这些信息,怎么办呢?比如说下面的HTML。

<div class = "container">
     <div class="wrapper">
        <div class = "jumbotron">
            <!-- adams data here -->
        </div>
        <div class = "jumbotron">
            <!-- johns data here -->
        </div>
    </div>
</div>

我知道如何查询数据库以获取PHP变量的信息,我只是不确定如何在单独的div中动态显示数据。

以下是我获取数据的方式

<?php 

    if($result = $db->query("SELECT forename,surname FROM users ")){
        if($count = $result->num_rows){
            while($row = $result->fetch_object()){
                echo $row->forename, '<br><br>';
                echo $row->surname, '<br><br>';
            }
            $result->free();
       }
    }
   ?>

2 个答案:

答案 0 :(得分:0)

将div包装在循环中,这样您就可以为每个结果行打印一个div

<?php foreach($result as $r): ?>
   <div class = "jumbotron">
     <?php echo $r['name'] // Print fields you need ?>
   </div>
<?php endforeach; ?>

编辑:现在我可以看到你的查询了。试试这个:

<?php 
  if($result = $db->query("SELECT forename,surname FROM users ")){
    if($count = $result->num_rows){
      while($row = $result->fetch_object()){
?>
        <div class = "jumbotron">
          <?php echo $row->forename; ?><br><br>
          <?php echo $row->surname; ?><br><br>
        </div>
<?php          
      }
    $result->free();
    }
  }
?>

答案 1 :(得分:0)

乔,这是一个相当简单的问题,可能已经得到了回答。 我将使用org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final} Oct 11, 2015 12:36:45 AM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.3.5.Final} Oct 11, 2015 12:36:45 AM org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found Oct 11, 2015 12:36:45 AM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist Oct 11, 2015 12:36:45 AM org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: ./resources/hibernate.cfg.xml Oct 11, 2015 12:36:45 AM org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: ./resources/hibernate.cfg.xml Initial SessionFactory creation failed.org.hibernate.HibernateException: ./resources/hibernate.cfg.xml not found Exception in thread "main" java.lang.ExceptionInInitializerError at tableSimulation.HibernateSessionManager.buildSessionFactory(HibernateSessionManager.java:33) at tableSimulation.HibernateSessionManager.<clinit>(HibernateSessionManager.java:11) at tableSimulation.Run.main(Run.java:14) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) Caused by: org.hibernate.HibernateException: ./resources/hibernate.cfg.xml not found at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173) at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2093) at org.hibernate.cfg.Configuration.configure(Configuration.java:2074) at tableSimulation.HibernateSessionManager.buildSessionFactory(HibernateSessionManager.java:23) 类的php

首先在文件中创建一个mysqli连接对象以获得最佳实践,然后将其包含在主脚本中。

mysqli

在您的主脚本中包含此内容

<?php
$connection = new mysqli($host_address, $username, $password, $database);
?>

您的结果应该是

<?php require "path\to\connection\script"; ?>
<?php
 $sql_adam = "SELECT * FROM persons WHERE forename = 'adam'";
 $sql_john = "SELECT * FROM persons WHERE forename = 'john'";

 $qry1 = $connection->query($sql_adam);
 $qry2 = $connection->query($sql_john);

?>
<div class = "container">
    <div class="wrapper">
        <div class = "jumbotron">
             <?php if ($qry1->num_rows >= 1){
                while($adam = $qry1->fetch_assoc()){
                    foreach ($adam as $column => $data) {
                       echo  "<p>$column : $data </p>";
                    }
                }
             }
        </div>
        <div class = "jumbotron">
            <?php if ($qry2->num_rows >= 1){
                while($john = $qry2->fetch_assoc()){
                    foreach ($john as $column => $data) {
                       echo  "<p>$column : $data </p>";
                    }
                }
             }
        </div>
    </div>
</div>