0

I have a PHP script that tests various functions of a LAMP environment, namely phpmail, mysql connections, and a phpinfo. For some reason, whenever I test the database connection and submit the form, no results are displayed. However, if you check the source, the results are shown in the source text but don't come up on the page itself. I've been looking at this all weekend and I'm at a loss.

Pastebin here: https://pastebin.com/SsKyFutK

DB Connection code here:

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

If anyone out there could lend a hand, I'd greatly appreciate it.

edit: Here's the code in the source that does not show up on the rendered page, from a test account on 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>
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Mar 05 '18 at 17:43
  • If you see it in the source you should see it on the page unless your CSS is displaying the information "off screen" – Jay Blanchard Mar 05 '18 at 17:46
  • Your script actually works fine on my server. If i leave everything blank, it connects but (obviously) reports access is denied. If i fill in credentials, it connects and lists the tables in the database.... What result are you getting? – Lucas Krupinski Mar 05 '18 at 17:50
  • I'm getting a blank screen to the right where the tables should be. The source shows the proper result as well, but not on the page itself. – William Marlett Mar 05 '18 at 18:01

0 Answers0