【问题标题】:How can I display a database table list to a URL?如何将数据库表列表显示到 URL?
【发布时间】:2017-10-19 15:55:22
【问题描述】:

我们想要显示来自数据库‘X’中一个表的列表链接。 这是我的代码:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>database connections</title>
</head>
<body>
  <?php
  $username = "root";
  $password = "mysql";
  $host = "localhost";

  $connector = mysql_connect($host,$username,$password)
      or die("Unable to connect");
    echo "Connections are made successfully::";
  $selected = mysql_select_db("nentholbenin", $connector)
    or die("Unable to connect");

  //execute the SQL query and return records
  $result = mysql_query("SELECT * FROM utilisateurs "); 
  ?>
  <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
  <thead>
    <tr>
      <th>Categories</th>
    </tr>
  </thead>
  <tbody>
    <?php 

      $db_result = mysql_query("SELECT Categories FROM utilisateurs"); 
      $result = $db_result; echo '<ul>';

      foreach($array as $index => $db_result){
       echo '<li><a href=".'$db_result['Categories'].'"</a></li>';
       }
       echo '</ul>';
    ?>
  </tbody>
</table>
 <?php mysql_close($connector); ?>
</body>
</html>

我收到此错误:

解析错误:语法错误,意外的“$db_result”(T_VARIABLE), 期待 ',' 或 ';'在

【问题讨论】:

  • 点应该在:'.$db_result['Categories'].'

标签: html mysql hyperlink html-table


【解决方案1】:

试试这个:

 <?php 

          $db_result = mysql_query("SELECT Categories FROM utilisateurs"); 
          $result = $db_result; echo '<ul>';

          foreach($array as $index => $db_result){
           echo '<li><a href="'.$db_result['Categories'].'"</a></li>';
           }
           echo '</ul>';
    ?>

【讨论】:

  • 谢谢你桑尼!但我收到此错误警告:为 foreach() 提供的参数无效
【解决方案2】:

您忘记了 $db_result 之前的点。 而且使用mysql_query执行查询后不能直接获取数据。使用 mysql_fetch_array 或 mysql_fetch_assoc

试试下面的代码

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>database connections</title>
    </head>
    <body>
      <?php
      $username = "root";
      $password = "mysql";
      $host = "localhost";

      $connector = mysql_connect($host,$username,$password)
      or die("Unable to connect");
        echo "Connections are made successfully::";
      $selected = mysql_select_db("nentholbenin", $connector)
      or die("Unable to connect");

      //execute the SQL query and return records
      $result = mysql_query("SELECT * FROM utilisateurs "); 
      ?>
    <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
        <thead>
        <tr>
            <th>Categories</th>
        </tr>
        </thead>
        <tbody>
        <?php

        $db_result = mysql_query("SELECT Categories FROM utilisateurs");
        $result = $db_result; echo '<ul>';

         while($array = mysql_fetch_assoc($result)) {
         echo '<li><a 
 href="/'.$array['Categories'].'">'.$array['Categories'].'</a></li>';
           }
        echo '</ul>';
        ?>
        </tbody>
    </table>
    <?php mysql_close($connector); ?>
    </body>
    </html>

【讨论】:

  • 干得好!但不显示项目类别链接,只显示标签(类别)。我想要做的是链接与用户帖子相关的类别和子类别。所以如果用户选择'categorie',里面的所有列表链接都会显示出来。
  • @NentholDigital 锚标签未正确关闭我已更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 2013-11-10
  • 1970-01-01
相关资源
最近更新 更多