-2

I created a database table using phpmyadmin and MySQL. Im trying to establish a connection to my database using a PHP script. Ive searched all alone and have found the right syntax and script to establish a simple connection. When i run the php file on my live server, the browser simply displays the actual code of the php file rather than a error message or any output.

Here is my php script that i am working with.

<?php

$username = "*******";
$password = "********";
$hostname = "*********"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
echo "Connected to MySQL <br>";

?>

Any help or direction as to what might be causing me to have absolutely no output?

Chris
  • 5,571
  • 2
  • 20
  • 32
BC0148
  • 49
  • 11
  • 3
    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 Apr 21 '16 at 18:22
  • 1
    Are you sure you have an apache server started? – Mihai Apr 21 '16 at 18:23
  • get the *actual error with `or die(mysql_error())` . Or you can find the issues in your current error logs. – Jay Blanchard Apr 21 '16 at 18:24
  • are you sure that the extension of the page is `.php`? also check whether your server has apache installed.. – Lal Apr 21 '16 at 18:24
  • Yes i am sure i have .php file extension – BC0148 Apr 21 '16 at 19:43
  • in regards to the apache server, im not sure i have that started. How can i find that out? – BC0148 Apr 21 '16 at 19:45

1 Answers1

-1

Because my duplicate flag was disputed despite this entire question being about PHP not rendering and the raw source being displayed instead I'm just going to quote @schmeeps' original answer. It's very thorough and covers a lot of the possible reasons PHP might not be being processed. If you found it useful, upvote the original answer here.

Sounds like there is something wrong with your configuration, here's a few things you can check:

  1. Make sure that PHP is installed and running correctly. This may sound silly, but you never know. An easy way to check is to run php -v from a command line and see if returns version information or any errors.

  2. Make sure that the PHP module is listed and uncommented inside of your Apache's httpd.conf This should be something like LoadModule php5_module "c:/php/php5apache2_2.dll" in the file. Search for LoadModule php, and make sure that there is no comment (;) in front of it.

  3. Make sure that the http.conf file has the PHP MIME type in it. This should be something like AddType application/x-httpd-php .php. This tells Apache to run .php files as PHP. Search for AddType, and then make sure there is an entry for PHP, and that it is uncommented.

  4. Make sure your file has the .php extension on it, or whichever extension specified in the MIME definition in point #3, otherwise it will not be executed as PHP.

  5. Make sure you are not using short tags in the PHP file (<?), these are deprecated not enabled on all servers by default. Use <?php instead (or enable short tags in your php.ini whith short_open_tag=On if you have code that relies on them).

  6. Make sure you are accessing your file over your webserver using an URL like http://localhost/file.php not via local file access file://localhost/www/file.php

And lastly check the PHP manual for further setup tips.

Community
  • 1
  • 1
Chris
  • 5,571
  • 2
  • 20
  • 32