0

I've installed WIndows 10 version of XAMPP with MySQL (MariaSQL) & PHP version 5.6.28

The SQL is working just fine, i.e., I can log in, create DATABASE, etc.

php -v works fine on the command line ... PHP 5.6.28 (cli)... phpinfo.php works as expected. It tells me display_errors is ON php.ini = display_errors=On

As a php script: mysql_test.php output to the screen is fine.

<?php
  echo "Hello World of PHP!";
  echo mysql_connect ('localhost', 'joe', 'gonzo9876');
?>

When I embed it in plain vanilla HTML, i.e., http://localhost/mysql_test.html The php code won't echo/print on the screen - and - when I right-click for viewing the source code, the php code is visible - and - the Google debugger has converted the php tags to

4
  • Aaah. You need server to run php. And php tags are never visible on page view source Commented Feb 21, 2017 at 4:17
  • My post was truncated: the php tags show in the debugger as <!--?php and ?--> Commented Feb 21, 2017 at 4:17
  • 1
    Possible duplicate of How to parse PHP syntax in a .html file on server? Commented Feb 21, 2017 at 4:19
  • Sampathkumar, I am running Apache server & connect as local host. php scripts run fine, but same script in HTML is invisible. Commented Feb 21, 2017 at 4:20

3 Answers 3

1

Your Apache, by default, will only run files with .php extension as PHP. .html will be displayed to browser as is.

You need to either:

  • Rename your file from mysql_test.html to mysql_test.php; or
  • Config your Apache to also treat .html files as PHP script

The later one is an unusual practice. I wouldn't recommend it.

Basically no hosting provider will do it. So even if you make it work in your XAMPP setup, it won't work in any normal shared hosting. So if you potentially need to move your code to a shared hosting, please don't do it.

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

4 Comments

Is "The later one is an unusual practice. Not recommended." personal opinion or documented?
A personal comment :-)
In my experience. No public hosting is doing it. That would make your code in-portable and hence "not recommended".
The personal comment detract from the objectivity, otherwise it's a correct answer.
1

You can't process the PHP code inside html page with .html extension(without parse). It's only for rendering html, if you wanna use embed/mix both php with html, then use .php extension instead as PHP is server-side scripting language. When talking about server-side language, you need a server either local(xampp,wampp,etc..)/production server to host and run your apps.

Reflect to Commenter's comment :

Another workarounds is by telling the Apache to treat .html as .php and with this, you can mix php code with html by using .html, but it's just kinda a HACK for me(personal perspective). Well the choice is yours.

3 Comments

Not true, you just need to tell Apache to handle html file through a php handler stackoverflow.com/questions/5532843/…
Nahh,,It's a hack, not the real use case. After all you need web server,
So it is incorrect to say "You can't process the PHP code inside html page"
0

You need to make extension .php if you want to put php code inside html tags. But if you do not want to show .php, please use .htaccess for url rewrite. You can make file as .php but with .htaccess you can show as .html so user will see it as .html.

RewriteEngine On
RewriteRule ^test.php test.html [R=301,L]

or something like this, please search for url rewrite for more detail.

1 Comment

So, I found this: AddType application/x-httpd-php .htm .html and tried it at the command line, but addType is not recognized as a command. Where do I insert the addType command? .

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.