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>
3
  • Change this index.html page to index.php page, and run it from the server again. Commented Jan 7, 2017 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 or pdo instead. And this is why you shouldn't use mysql_* functions. Commented Jan 7, 2017 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? Commented Jan 7, 2017 at 18:29

2 Answers 2

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

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

1 Comment

Totally forgot I couldn't run PHP off of my computer without a server running. Thanks.
-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>

3 Comments

I tried this code but it had errors on this line: $mysqlConnection = new PDO('mysql:host={$server};dbname={$database};', {$username}, {$password}); Thanks.
@tazboy what error message did you get, and i made a little correction to the script
I don't remember exactly but it said something about an error with '{'

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.