【问题标题】:How to enter enter the results from an sql query into a html table with php [duplicate]如何使用php将sql查询的结果输入到html表中[重复]
【发布时间】:2017-02-17 10:29:50
【问题描述】:

我正在寻找一种解决方案,它循环遍历我的数据库查询中的每条记录,并在具有固定表标题的 html 表中为其提供自己的行。以下是我用来查询数据并获取结果的当前 php。我的表格的每个标题名称是:PropertyType、Description、RentPrice 和 Location。但是由于某种原因,当我尝试运行我的代码时,屏幕上没有显示任何内容

<!DOCTYPE html>
<html lang="en">
<body>

<p style ="text-align: center" id="php_style">
<?php
    error_reporting(E_ERROR | E_PARSE);
    session_start();
    $counter_name = "counter.txt";
    // Check if a text file exists. If not create one and initialize it to zero.
    if (!file_exists($counter_name)) {
      $f = fopen($counter_name, "w");
      fwrite($f,"0");
      fclose($f);
    }
    // Read the current value of our counter file
    $f = fopen($counter_name,"r");
    $counterVal = fread($f, filesize($counter_name));
    fclose($f);
    // Has visitor been counted in this session?
    // If not, increase counter value by one
    if(!isset($_SESSION['hasVisited'])){
      $_SESSION['hasVisited']="yes";
      $counterVal++;
      $f = fopen($counter_name, "w");
      fwrite($f, $counterVal);
      fclose($f); 
    }
    echo  nl2br ("You are visitor number ".$counterVal. " to this site \n Today's date is ". date("d/m/Y"));





    $con=mysqli_connect("localhost:8889","root","root","booksmart_properties");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    else{

        echo "we connected";
    }

    // Perform queries 
$result=mysqli_query("SELECT * FROM ListedProperties"); ?> </p>



    <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
  <thead>
    <tr>
      <th>PropertyType</th>
      <th>Description</th>
      <th>RentPrice</th>
        <th>Location</th>
    </tr>
  </thead>
  <tbody>
    <?php
      while( $row = mysqli_fetch_assoc( $result ) ){
        echo
        "<tr>
          <td>{$row\['PropertyType'\]}</td>
          <td>{$row\['Description'\]}</td>
          <td>{$row\['RentPrice'\]}</td>
          <td>{$row\['Location'\]}</td>
        </tr>\n";
      }
    ?>
  </tbody>
</table>
 <?php mysql_close($connector); ?>


    //$result=mysqli_fetch_assoc($sql);

    //foreach($result as $key => $val) {
    //print $val; 
    //}

    //echo $result['*'];


    //mysqli_close($con);

//?>

我知道这可能是微不足道的,但我是 php 新手,希望能得到所有帮助。

【问题讨论】:

  • &lt;?php mysql_close($connector); ?&gt; //$result=mysqli_fetch_assoc($sql); ... //?&gt; 不知道为什么你会喜欢它,这应该会引发解析错误,$row\['PropertyType'\] 等也会引发解析错误。

标签: php html mysql web-deployment


【解决方案1】:

这是一个简单的解决方案:

while ( $row = mysqli_fetch_assoc( $result ) ){?>
    <tr>
      <td><?=$row['PropertyType']?></td>
      <td><?=$row['Description']?></td>
      <td><?=$row['RentPrice']?></td>
      <td><?=$row['Location']?></td>
    </tr>
<?php
}

【讨论】:

  • 我是否必须在所有这些 php 代码的开口处写 'php'?
  • 您已经在提供的代码中编写了它们。
  • &lt;?= $x ?&gt;&lt;?php echo $x ?&gt; 的简写 - 但它不一定在您的 php 上启用.. 请参阅 stackoverflow.com/questions/1386620/…
猜你喜欢
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多