PHP环境测试脚本不显示数据库连接结果

时间:2018-03-05 17:35:21

标签: php

我有一个PHP脚本,用于测试LAMP环境的各种功能,即phpmail,mysql连接和phpinfo。出于某种原因,每当我测试数据库连接并提交表单时,都不会显示任何结果。但是,如果您检查来源,结果会显示在源文本中,但不会出现在页面本身上。我整个周末都在看这个,而且我不知所措。

在这里粘贴:https://pastebin.com/SsKyFutK

数据库连接代码:

  <div id="test_area" class="col-sm-8 tab-content">
    <div class="dbConnectionTest tab-pane" id="db-connect" role="tabpanel" aria-labelledby="db-connect-link">
      <?php
        // Check to see if the form was submitted
          if (isset($_POST['host'])) {
            $dbhost     = $_POST['host'];
            $dbname   = $_POST['database'];
            $dbuser     = $_POST['user'];
            $dbpass     = $_POST['pwd'];
            $dbport      = $_POST['port'];
            if (strlen($dbport) < 4) { $dbport = 3306; }
            if (!function_exists('gethostname')) {
                $hostname = `hostname`;
                $hostnamearray = explode('.', $hostname);
                $hostname = $hostnamearray[0];
            }
            else $hostname = gethostname();
            // Check to see if mysqli is available
            if (!extension_loaded('mysqli') or $_POST['config'] == "mysql") {
                $dbhost = $dbhost.':'.$dbport;
                // Create connection
                $con = @mysql_connect($dbhost,$dbuser,$dbpass);
                // Check connection
                if (mysql_error()) {
                    die("Failed to connect to MySQL using the PHP mysql extension: " . mysql_error());
                }
                mysql_select_db($dbname, $con);
                // Query the database to show all the tables.
                $query = 'SHOW tables;';
                $result = mysql_query($query);
                // Print the results of the query.
                echo "Here is a list of the tables in your database:<br>";
                echo "- $dbname <br>";
                while($row = mysql_fetch_array($result)) {
                    echo "\ _ _ $row[0] <br>";
                }
                echo "<br>The connection to \"$dbname\" was successful!";
                $extension = "MySQL";
                mysql_close($con);
            }
            else {
                // Create connection
                $con = @mysqli_connect($dbhost,$dbuser,$dbpass,$dbname,$dbport);
                // Check connection
                if (mysqli_connect_errno()) {
                    die ("Failed to connect to MySQL using the PHP mysqli extension: " . mysqli_connect_error());
                }
                // Query the database to show all the tables.
                $query = 'SHOW tables;';
                $result = mysqli_query($con, $query);
                // Print the results of the query.
                echo "Here is a list of the tables in your database:<br>";
                echo "- $dbname <br>";
                while($row = mysqli_fetch_array($result)) {
                    echo "\ _ _ $row[0] <br>";
                }
                echo "<br>The connection to \"$dbname\" was successful!";
                $extension = "MySQLi";
                mysqli_close($con);
            }
            echo "<br>PHP extension used: " . $extension;
            echo "<br>Server Name: " . $hostname;
            unset($_POST);
         }
         // If we are not here because of a form submission, do the following.
        else { ?>

        <div class="card">
          <div class="card-body">
            <h3 class="card-title">Database Connection Test</h3>
            <form method="post">
              <div class="form-group row">
                <label class="col-sm-3 col-form-label" for="config">Connection:</label>
                <div class="col-sm-9">
                  <select class="form-control form-control-sm" name="config" id="config">
                    <option value="auto" selected>Auto Select</option>
                                    <option value="mysqli">MySQLi</option>
                                    <option value="mysql">MySQL</option>
                  </select>
                </div>
              </div>
              <div class="form-group row">
                <label class="col-sm-3 col-form-label" for="host">Host Name:</label>
                <div class="col-sm-9">
                  <input type="text" class="form-control form-control-sm" id="host" name="host" autofocus>
                </div>
              </div>
              <div class="form-group row">
                <label class="col-sm-3 col-form-label" for="database">DB Name:</label>
                <div class="col-sm-9">
                  <input type="text" class="form-control form-control-sm" id="database" name="database">
                </div>
              </div>
              <div class="form-group row">
                <label class="col-sm-3 col-form-label" for="user">User:</label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control form-control-sm" id="user" name="user">
                  </div>
              </div>
              <div class="form-group row">
                <label class="col-sm-3 col-form-label" for="pwd">Password:</label>
                <div class="col-sm-9">
                  <input type="password" class="form-control form-control-sm" id="pwd" name="pwd">
                </div>
              </div>
              <div class="form-group row">
                <label class="col-sm-3 col-form-label" for="port">Port (optional):</label>
                  <div class="col-sm-9">
                    <input type="nubmer" class="form-control form-control-sm" id="port" name="port" min="1" max="65535" value="3306">
                  </div>
              </div>
              <div class="text-center">
                <button type="submit" class="btn btn-primary">Connect</button>
              </div>
            </form>
          </div>
        </div>
      <?php
        }
     ?>
    </div>

如果那里的任何人可以伸出援手,我会非常感激。

编辑:这是源代码中未显示在渲染页面上的代码,来自GD上的测试帐户:

<body>
<header>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
  <span class="navbar-brand mb-0 h1">Does PHP Work?</span>
</nav>
</header>
<div class="container-fluid wt-mt">
<div class="row">
  <div class="col-sm-2 text-center">
    <div class="nav flex-column" role="tablist" aria-orientation="vertical">
      <a class="nav-link" id="db-connect-link" data-toggle="pill" href="#db-connect" role="tab" aria-controls="db-connect" aria-selected="true">DB Connection Test</a>
      <a class="nav-link" id="form-mail-link" data-toggle="pill" href="#form-mail" role="tab" aria-controls="form-mail" aria-selected="false">Form Mailer Test</a>
      <a class="nav-link" id="php-info-link" data-toggle="pill" href="#php-info" role="tab" aria-controls="php-info" aria-selected="false">PHP Info</a>
    </div>
  </div>
  <div id="test_area" class="col-sm-8 tab-content">
    <div class="dbConnectionTest tab-pane" id="db-connect" role="tabpanel" aria-labelledby="db-connect-link">
      Here is a list of the tables in your database:<br>- hurdywordy <br>\ _ _ wp_commentmeta <br>\ _ _ wp_comments <br>\ _ _ wp_links <br>\ _ _ wp_options <br>\ _ _ wp_postmeta <br>\ _ _ wp_posts <br>\ _ _ wp_term_relationships <br>\ _ _ wp_term_taxonomy <br>\ _ _ wp_termmeta <br>\ _ _ wp_terms <br>\ _ _ wp_usermeta <br>\ _ _ wp_users <br><br>The connection to "hurdywordy" was successful!<br>PHP extension used: MySQLi<br>Server Name: p3plcpnl0520.prod.phx3.secureserver.net        </div>

0 个答案:

没有答案