PHP脚本连接到数据库,但是尽管已连接,但不会返回表值。

时间:2015-08-14 03:22:14

标签: php mysql

        <!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。不确定这是否与任何事情有关。

1 个答案:

答案 0 :(得分:1)

应该是这个

在您的数据库配置

$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
}