0

I am trying to run this simple php code (in a file named "database.php")

<html>
<head>
</head>
<body>
<?php
        $servername = 'localhost';
        $username = 'admin';
        $password = 'admin1';

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

    if ($conn->connect_error) {
    die('Connection failed: ' . $conn->connect_error);

                             } 
else { 

echo 'Connected Successfully.' ;

     }

$conn->close();

    ?>
</body>
</html>

AND HERE IS THE OUTPUT IN MY BROWSER

connect_error)  
{ die('Connection failed: ' . $conn->connect_error); } 
else { echo 'Connected Successfully.' ; } 
$conn->close();
?>

( the first part of php code get processed but later it shows just the source code)

PLEASE HELP.

13
  • Install a web server with PHP. Commented Oct 26, 2014 at 16:17
  • You also need to pass a 4th parameter $conn = new mysqli($servername, $username, $password, $db_name); Commented Oct 26, 2014 at 16:20
  • @Fred-ii- I am running wamp server and PHP worked quite well until a few hours back.. it was successfully connecting to database but suddenly it stopped working. Thanks in advance. please help Commented Oct 26, 2014 at 16:25
  • tried that here is my new code.. still same output..<html> <head> </head> <body> <?php $servername = 'localhost'; $username = 'admin'; $password = 'admin1'; $db_name = 'azadhind'; //Create connection $conn = new mysqli($servername, $username, $password, $db_name); // Check connection if ($conn->connect_error) { die('Connection failed: ' . $conn->connect_error); } else { echo 'Connected Successfully.' ; } $conn->close(); ?> Commented Oct 26, 2014 at 16:27
  • 1
    @Ramesh - changing the code will NOT fix it. NONE of your code is being executed, see my explanation why some code is invisible below. For whatever reason, Apache is not processing PHP. If you had a power failure, perhaps a config file was corrupted or something. Commented Oct 26, 2014 at 16:53

2 Answers 2

1

Set a handler for *.php files in the webserver config. For apache:

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Sign up to request clarification or add additional context in comments.

2 Comments

how do I do that , I am using wampserver
Not really familiar with wampserver, but I know it uses apache. Also have no idea why it's not set by default. You should look for a conf or conf.d directory with httpd.conf in it. My snipped should be placed inside <Directory /> tag.
0

The first part of the code is not actually being processed, but being hidden because the browser thinks it is an HTML tag. So everything between <? and -> gets hidden. A viktike points out, you need to enable PHP parsing by your server, which may require additionally a number of actions beyond simply adding PHP handing in the sercver config.

I'm not familiar with Wamp, but it seems like PHP has been disabled so I'd explore re-enabling, starting with simply restarting the server, and then looking at Apache config and Wamp's own method of configuring things which probably departs from standard method of doing this stuff.

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.