0

I am fairly new to HTML and PHP, i know SQL and that's what i'm trying to use here (in PHP).

This is what i am using to create a simple connection to the SQL server, but this won't even work:

<html>
<h1>Test</h1>
<?php
$servername = "localhost";
$username = "username"; // Ofc with my SQL details.
$password = "password";

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

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

But it get to this part connect_error) { and from there it just displays the code. Example:

enter image description here

^^ This is what comes up.

I think it has something to do with the PHP as it looks as if it's getting to the > and then thinking that's ending the PHP or something. Then again i could also be doing thing completely wrong - like i said, i'm new to HTML and PHP.

Thanks for any help.

Pete
  • 57,112
  • 28
  • 117
  • 166
Erouax
  • 163
  • 2
  • 10
  • 1
    Your assumption is right, PHP isn't being executed. – castis Oct 14 '15 at 13:33
  • 1
    Are you using a server to execute this ? You can't just execute PHP code directly into the browser (you can't just drag & drop a file into your browser) – Alexandre Beaudet Oct 14 '15 at 13:34
  • 2
    Make sure you're browsing `http://` not `file://`. Second, if that code is in a .html file, make sure Apache executes it as .php as well. – Alex Tartan Oct 14 '15 at 13:36
  • 1
    Are you connect to the server ? – Maduro Oct 14 '15 at 13:37
  • @AlexTartan I tried with http:// but it just says ERR_NAME_NOT_RESOLVED – Erouax Oct 14 '15 at 13:40
  • @AlexandreBeaudet I'm using my browser, not drag & drop, but opening it normally. Is this a problem? – Erouax Oct 14 '15 at 13:41
  • Check my answer about Apache server, you need a server in order to use PHP ! It's not like javascript – Alexandre Beaudet Oct 14 '15 at 13:41
  • 1
    I think that you should watch some tutorials in php first, if you are having problem with the connection, then you may have problems with anything else in your way =/ such that displaying data from the database, or inserting things to the database – Maduro Oct 14 '15 at 13:43

2 Answers2

0

PHP files first need be processed in a web server before sending their output to the web browser. - See more at: http://www.phpknowhow.com/basics/running-php-files/#sthash.rtr1MtAA.dpuf To start using PHP, you can:


  1. Find a web host with PHP and MySQL support

OR/AND

  1. Install a web server on your own PC, and then install PHP and MySQL

Since you're not talking about an apache server, I assume you're not using one !

In order to execute PHP, you need a server processing it. You can check this tutorial/course : http://www.w3schools.com/php/php_install.asp

Alexandre Beaudet
  • 2,774
  • 2
  • 20
  • 29
0

Make sure that your file is a PHP file. Check the file extension to be .php. Also note that the 4th parameter for MySQLi class is the database name that should be used. That being said, the index.php file should look like this:

<html>
<h1>Test</h1>
<?php
$host     = "localhost";
$username = "username"; // Ofc with my SQL details.
$password = "password";
$dbName   = "dbName";

$mysqli = new MySQLi($host, $username, $password, $dbName);

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}
?>
<h2></h2>
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50