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

标签 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 表的“subForum”行中回显 HTML $row['subForum'],但是,由于某种原因,它不是这样正在连接。

即使在我的数据库配置文件 dbforum.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 脚本正在连接到数据库,但尽管已连接,但不会返回表值。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32001766/

相关文章:

PHP 错误地处理了我的静态调用

php - 在sql中添加新行

java - 使用预定义的scrypt文件通过Spring自动生成sql表

php - UTF-8贯穿始终

PHP/mySQL 获取时差 x 秒

php - 带有 PHP 选择器的 cPanel 显示 WordPress 错误 "PHP installation appears to be missing the MySQL extension which is required by WordPress"

php - 创建类的集合方法的正确方法

mysql - 其中公司 =(不同公司)

html - 使用mysql和html的nodejs登录表单

java - JPA/Hibernate 适用于 Postgresql,但不适用于 Mysql