0

Here is my index.html file. I load the page and nothing happens. Shouldn't it print "Please try again" on the webpage if my info is incorrect?

 <html>
    <body>
      <h1>mySQL</h1>

      <?php

      $server = "mysql.blah.com"; 
      $username = "my_username";
      $password = "my_password";
      $database = "my_database";

      $mysqlConnection = mysql_connect($server, $username, $password);
      if (!$mysqlConnection){
        echo "Please try later.";
      }
      else {
        echo "All good";
        mysql_select_db($database, $mysqlConnection);
      }

      ?>

    </body>
</html>
tazboy
  • 1,685
  • 5
  • 23
  • 39
  • Change this *index.html* page to *index.php* page, and run it from the server again. – Rajdeep Paul Jan 07 '17 at 18:27
  • 1
    Don't use `mysql_*` functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`pdo`](http://php.net/manual/en/book.pdo.php) instead. [**And this is why you shouldn't use `mysql_*` functions**](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Rajdeep Paul Jan 07 '17 at 18:28
  • Did you change the handler to load `HTML` as `PHP`? What PHP version are you running? What do you error logs show? Is the PHP in your source? – chris85 Jan 07 '17 at 18:29

2 Answers2

5

This is because your file has the .html extension.
Change it to .php and run it again.

Be sure to run it on a web server, that has PHP installed

Alex
  • 37,502
  • 51
  • 204
  • 332
-1

change the file to the .php extension and use this refactored version

<html>
    <body>
      <h1>mySQL</h1>

      <?php

     try 
     {
        $server = "mysql.blah.com"; 
        $username = "my_username";
        $password = "my_password";
        $database = "my_database";

        $mysqlConnection = new PDO('mysql:host={$server};dbname={$database};', '{$username}', '{$password}');
        $mysqlConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      }
      catch(PDOException $e)
      {
        echo ('Please try later.');
        echo $e->getMessage();
      }
      ?>

    </body>
</html>
Francis Sunday
  • 77
  • 1
  • 11
  • I tried this code but it had errors on this line: `$mysqlConnection = new PDO('mysql:host={$server};dbname={$database};', {$username}, {$password});` Thanks. – tazboy Jan 07 '17 at 19:17
  • @tazboy what error message did you get, and i made a little correction to the script – Francis Sunday Jan 09 '17 at 14:27
  • I don't remember exactly but it said something about an error with `'{'` – tazboy Jan 10 '17 at 14:55