0

When I add the mysqli_connect statement in my php file it stops working the whole script. If make this statement comment my php file works as expected. I am adding this file to a wordpress website.

I have tried to connect using mysql_connect() also but the result is same.

My Code:

<?php

require_once ($_SERVER['DOCUMENT_ROOT'].'/wp-config.php');

ini_set('display_errors', 1); 
error_reporting(E_ALL);

//phpinfo();

echo DB_NAME ."<br>";
echo DB_USER ."<br>";
echo DB_PASSWORD ."<br>";
echo DB_HOST."<br>"


$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);
// database port is also 3306 which is default

if (!$conn) {
    echo  "Connection failed: " . mysqli_connect_error();
}
echo "Connected successfully";

?>

php.info() outputs that mysqli is installed is also enabled.

Do I need to include some file to access mysqli_connect() function.

How can I connect to the database?

6
  • Can you write whats error you are getting..? Commented Jul 8, 2017 at 8:05
  • 4
    You missed semi colon at echo DB_HOST."<br>" Commented Jul 8, 2017 at 8:06
  • @SaadSuri but the echo statements are fine and shows the proper output if I remove the mysqli_connect() and oter db related statements but in combinations I get nothing in output Commented Jul 8, 2017 at 8:18
  • Because you don't need to terminate the last line of code. It can be done through ?>. When you remove your all db related statements so echo DB_HOST."<br>" becomes the last statement which will be successfully terminated by ?> Commented Jul 8, 2017 at 8:20
  • @SaadSuri yes it worked for me. Commented Jul 8, 2017 at 8:26

2 Answers 2

1

You missed a semicolon at echo DB_HOST."

    require_once ($_SERVER['DOCUMENT_ROOT'].'/wp-config.php');

    ini_set('display_errors', 1); 
    error_reporting(E_ALL);

    //phpinfo();

    echo DB_NAME ."<br>";
    echo DB_USER ."<br>";
    echo DB_PASSWORD ."<br>";
    echo DB_HOST."<br>";


    $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);
    // database port is also 3306 which is default

    if (!$conn) {
        echo  "Connection failed: " . mysqli_connect_error();
    }
    echo "Connected successfully";

    ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Use this code. You missed semi colon at end of DB_HOST."<br>" line. Every thing is fine in code.

<?php

require_once ($_SERVER['DOCUMENT_ROOT'].'/wp-config.php');

ini_set('display_errors', 1); 
error_reporting(E_ALL);

//phpinfo();

echo DB_NAME ."<br>";
echo DB_USER ."<br>";
echo DB_PASSWORD ."<br>";
echo DB_HOST."<br>";


$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);
// database port is also 3306 which is default

if (!$conn) {
    echo  "Connection failed: " . mysqli_connect_error();
}
echo "Connected successfully";

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.