Yash Agarwal
Yash Agarwal

Reputation: 57

How do I hide the echo result from showing up on HTML?

For Instance consider this standard database connection php file.

db_conn.php

<?php

$servername = "localhost";
$username = "root";
$password = "Yash123";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

If connected successfully it is shows that result onto the website.

What I'd need it to do is not show up on the html but be there in the file so I can test the file executing in the CLI(Command Line Interface) of PHP.

I am using require_once(); in the index file.

Upvotes: 2

Views: 2930

Answers (2)

slevy1
slevy1

Reputation: 3832

One could easily dispense with the echo statement and instead return the connection object upon success. This would entail revising the OP code so that it becomes the contents of a database connect function. This idea I gleaned from binpress.com and include suggestions from the Manual, too:

<?php

/* Assuming that user name,password and database name are   
   credentials that come from a file outside of the 
   document root for security. */

 function db_connect() {
   static $conn;
   $servername = "localhost";

   if ( !isset( $conn ) ) {

      // load and parse file containing credentials:
      $config = parse_ini_file('../config.ini'); 

      // Create connection
      $conn = new mysqli( $servername,   $config['username'],$config['password'],
    $config['dbname']);

      // Check connection
      if ($conn->connect_error) {
          die('Connect Error (' . $conn->connect_errno . ') '
                  . $conn->connect_error);
       }

       // Connected successfully
       return $conn;
  }

Upvotes: 0

Rotimi
Rotimi

Reputation: 4825

uncomment the echo line. or use php's error_log('Connected Successfully');. this would log that the connection was successful. This would hide output from your html and log the string passed as parameter to your error_log file

Upvotes: 3

Related Questions