<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="https://storage.googleapis.com/code.getmdl.io/1.0.2/material.indigo-pink.min.css">
        <script src="https://storage.googleapis.com/code.getmdl.io/1.0.2/material.min.js"></script>
        <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    </head>
    <body>
        <!-- Always shows a header, even in smaller screens. -->
        <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
          <header class="mdl-layout__header">
            <div class="mdl-layout__header-row">
              <!-- Title -->
              <span class="mdl-layout-title">Forum</span>
              <!-- Add spacer, to align navigation to the right -->
              <div class="mdl-layout-spacer"></div>
              <!-- Navigation. We hide it in small screens. -->
              <nav class="mdl-navigation mdl-layout--large-screen-only">
                <a class="mdl-navigation__link" href="">Default Subforum2</a>
              </nav>
            </div>
          </header>
          <div class="mdl-layout__drawer">
            <span class="mdl-layout-title">Forum</span>
            <nav class="mdl-navigation">
              <a class="mdl-navigation__link" href="">Browse Subforums</a>
              <a class="mdl-navigation__link" href="">Search For Post</a>
            </nav>
          </div>
          <main class="mdl-layout__content">
            <div class="page-content">
                <!-- Your content goes here -->
                <?php
                    include("dbforforum.php");
                    $subForum = "";
                    $results = $mysqli->query("SELECT * FROM `SubForumList`");
                    echo $results;
                    for ($i = 0; $i < $results->num_rows; $i++)
                    {
                        $results->data_seek($i);
                        $row = $results->fetch_assoc();
                        $subForum = $row['Name'];
                        echo $subForum;
                        echo $subForum;
                    }
                ?>
            </div>
          </main>
        </div>
    </body>
</html>


每当此循环通过时,我都试图在PHP中的SQL表$row['subForum']的'subForum'行中回显HTML,但是由于某种原因,它没有连接。

即使在我的数据库配置文件dbforforum.php中,它也显示已成功连接:

<?php

$mysqli = new mysqli("localhost", "root", "", "Forum");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

?>


我对此感到非常困惑。任何帮助表示赞赏。

注意:我最近从Windows切换到Mac(不是我的选择),因此从WAMP转到MAMP。不知道这是否与任何事情有关。

最佳答案

应该是这个

在您的数据库配置中

$mysqli = new mysqli("localhost", "root", "", "Forum"); //the variable here is mysqli
 if ($conn->connect_error) { //the error here is $conn because it isn't the variable above so change the variable to $mysqli
  die("Connection failed: " . $conn->connect_error); //and here too to $mysqli
 }
 echo "Connected successfully";


变化:
    

$mysqli = new mysqli('localhost', 'root', '', 'databasename'); //connect db

if($mysqli->connect_errno){
  die("Error! ".$mysqli->connect_errno);
}

if($result = $mysqli->query("SELECT * FROM SubForumlist")){
  echo $result->num_rows //num_rows == your data you want to echo
}

关于php - PHP Script正在连接到数据库,但即使已连接也不会返回表值。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32001766/

10-11 03:21
查看更多