0

My code is very simple. I'm attempting to connect to a MySQL instance using pdo_mysql functions. I'm thinking there is a syntax error but the dozens of websites I researched doesn't help me identify it. Below is my code and what is presented on the webpage. Can someone give me some direction?

*** I tried to insert some sites but the app is not letting me.

The pdo_mysql extension is enabled and is displayed in phpinfo.php.

I'm using PHP 7.1.4 on a Windows Server 2012R2 with IIS 8.5

FastCGI is enabled.

Here is my code:

<?php
$username = "myusername";
$password = "mypassword"
$dbname = "mysql"
$hostname = "myserver"
$query = "select user,host from mysql.user";
try {
    $dbconn = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
$dbconn = null;
?>

The results on the webpage is:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } $dbconn = null; ?> 
jprichart
  • 3
  • 3

1 Answers1

0

Based on the fact that your webpage is outputting all of the text after the >, I'm inclined to think that this file is being interpreted as an HTML document, rather than being processed as PHP.

If you view the source for the web page, I would bet that you would see everything from <?php to $dboconn-> in the source, but treated as one long html element.

The obvious thing to check first is if the file has a .php extension. If not, then you'll need to dig into why your server is not executing .php files.

RToyo
  • 2,877
  • 1
  • 15
  • 22