【问题标题】:Pull database info and display to html problem?拉取数据库信息并显示到 html 问题?
【发布时间】:2019-02-13 19:57:49
【问题描述】:

我正在尝试将数据库中的信息显示到 html 页面,但它只显示“连接成功”的消息。数据库的名称是“admin”,该数据库中的表称为“users”。我不知道如何绕过这条消息,只显示我创建的表格。

索引页(index.php):

<?php
include_once('connection.php');
$query="select * from users";
$result=mysql_query($query);
?>
<!DOCTYPE html>
<html>
<title>
    <head> Fetch Data Frome Database</head>
</title>
<body>

<table align="center" border="1px" style="width:250px;       line-height: 30px;">
    <tr>
        <th colspan="4"><h2>Account Record</h2></th>
    </tr>
    <t>
        <th>ID</th>
        <th>Username</th>
        <th>Password</th>
    </t>

    <?php

    while($rows=mysql_fetch_assoc($result))
    {
    ?>

    <tr>
        <td><?php echo $rows['ID'];?></td>
        <td><?php echo $rows['username'];?></td>
        <td><?php echo $rows['password'];?></td>
    </tr>
    <?php
    }

    ?>

</table>

</body>
</html>

连接页面(connection.php):

<?php
//Include your own values for username, password and     dbase name

$host = "localhost";
$username = "root";
$password = "root";
$dbname = "admin";

// Create connection
$conn = new mysqli($host, $username, $password,     $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

【问题讨论】:

  • 您使用 mysqli 连接,但在查询中使用了 mysql。
  • 谢谢。尽管这有助于将表格实际显示到网页上,但表格中的结果并未显示。我将查询 sql 更改为 sqli,将 while 循环更改为 sqli。我是否缺少任何实际显示结果的内容?
  • 我已经把我的编码改成了上面的^^^。这现在显示了表格,以及我在数据库中拥有的 3 组数据的 3 个插槽。仍然没有将其从数据库中提取出来
  • 警告:不要使用在 PHP 7 中删除的过时的mysql_query 接口。PDO is not hard to learn 之类的替代品和PHP The Right Way 之类的指南有助于解释最佳实践.这里的参数是 NOT properly escaped 并且在这段代码中有严重的 SQL injection bugs。转义任何和所有用户数据,尤其是来自$_POST$_GET

标签: php html mysql database fetch


【解决方案1】:

尝试将所有 PHP 放入一个块中

 <?php

include_once('connection.php');
$query="select * from users";
$result=mysql_query($query);

    while($rows=mysql_fetch_assoc($result))
    {


    echo "<tr>";
        echo "<td>".$rows['ID']."</td>";
    echo "<td>".$rows['username']."</td>";
    echo "<td>".$rows['password']."</td>";
    echo "</tr>";

}

?>

【讨论】:

  • 刚试了那个伴侣,结果还是一样。只显示没有数据库信息的表格和标题。我试图从数据库中调用的列的名称也匹配。想不出别的了。
  • var_dump 或 print_r $result 会得到什么?让我们验证数据是否真正通过。
  • 对不起,我以前从未尝试过。你能解释一下如何检查吗?
  • 简单的 var_dump($result);或 print_r($result);
  • @您确定数据库中有记录吗?
【解决方案2】:

第一个问题是您的 while 循环设置不正确。 equals 运算符会将 mysql_fetch_assoc($result) 的值传递给 $rows 所以这不会做任何事情。只是认为单个等号并不意味着等于它实际上意味着成为。所以行成为结果。如果您有两个等号,则您正在比较两个值,但这也无济于事。您真正需要做的是首先将行设置为零,然后说当行小于结果时回显 id 回显用户名 回显密码并将行加 1。

所以while循环应该更像这样

$rows = 0;
while($rows < mysql_fetch_assoc($result) ){ 
         $rows++   
   ?>

<tr>
    <td><?php echo $rows['ID'];?></td>
    <td><?php echo $rows['username'];?></td>
    <td><?php echo $rows['password'];?></td>
</tr>
<?php
    }
}

假设 $result=mysql_query($query) 实际上有一个值,这应该可以工作。

如果您需要测试您的结果是否良好,请使用 PHP 的数组 print 来检查 print_r($result);这会将所有结果回显到屏幕上。

【讨论】:

    【解决方案3】:

    所以我更喜欢使用这种方法,因为它比我尝试过的任何其他方法都好一点。另外,我将连接放入一个类中,这使得处理数据库连接变得更加容易。

    数据库连接页面:

    class Database
    {
    private $host = "localhost";
    private $db_name = "root";
    private $username = "root";
    private $password = "admin";
    public $conn;
    
    public function dbConnection()
    {
    
        $this->conn = null;
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }
    
        return $this->conn;
    }
    

    }

    现在我为连接创建一个类。我通常将其命名为站点名称。

    require_once "db/config/page/location.php";
    
    class localhost {
      // Create conn to db
          private $conn;
    
          public function __construct()
          {
            $database = new Database();
            $db = $database->dbConnection();
            $this->conn = $db;
          }
    
          public function runQuery($sql)
          {
            $stmt = $this->conn->prepare($sql);
            return $stmt;
          }
    }
    

    注意:当您想在任何页面上使用连接时。 您可以执行以下操作: EG 索引页:

    <?php
        include_once('class.php');
        $lHost = new localhost();
    
            //calling the users
            $stmt = $lHost->runQuery('SELECT * FROM `users`');
            $stmt->execute();
            $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
        ?>
        <!DOCTYPE html>
        <html>
        <title>
            <head> Fetch Data Frome Database</head>
        </title>
        <body>
    
        <table align="center" border="1px" style="width:250px;       line-height: 30px;">
            <tr>
                <th colspan="4"><h2>Account Record</h2></th>
            </tr>
            <t>
                <th>ID</th>
                <th>Username</th>
                <th>Password</th>
            </t>
    
            <?php
            foreach ($users as $key => $user) {
            ?>
            <tr>
                <td><?php echo $user['ID'];?></td>
                <td><?php echo $user['username'];?></td>
                <td><?php echo $user['password'];?></td>
            </tr>
            <?php
            }
            ?>
    
        </table>
    
        </body>
        </html>

    如有任何问题,请随时问我。

    【讨论】:

      【解决方案4】:

      在你的文件中试试这个。使用此文件作为第一个进行测试。

      <html>
         <head>
            <title>Selecting Table in MySQLi Server</title>
         </head>
      
         <body>
            <?php
               $dbhost = 'localhost:3306';
               $dbuser = 'root';
               $dbpass = '';
               $dbname = 'admin';
               $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
      
               if(! $conn ) {
                  die('Could not connect: ' . mysqli_error());
               }
               echo 'Connected successfully<br>';
               $sql = 'SELECT name FROM tutorials_inf';
               $result = mysqli_query($conn, $sql);
      
               if (mysqli_num_rows($result) > 0) {
                  while($row = mysqli_fetch_assoc($result)) {
                     echo "Name: " . $row["name"]. "<br>";
                  }
               } else {
                  echo "0 results";
               }
               mysqli_close($conn);
            ?>
         </body>
      </html>
      

      注意:您可以在选择查询后使用mysqli_error()(例如echo mysqli_error();)来测试错误。

      【讨论】:

        猜你喜欢
        • 2013-11-03
        • 2016-10-25
        • 2014-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-16
        • 1970-01-01
        相关资源
        最近更新 更多