I am trying to connect to a MySQL database usign PHP but am currently facing this error : Parse error: syntax error, unexpected '$servername' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) in line 4.
Here is my code :
$servername = "localhost";
$username = "root";
$password = "anything";
try {
$bdd = new PDO('mysql:host=$servername;dbname=myDB;charset=utf8', $username, $password); //connexion à MySQL
// set the PDO error mode to exception
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
$stmt = $bdd->prepare("SELECT Place, Address, Date FROM MyDB");
$stmt->execute();
}
catch(Exception $e)
{
die('Connection failed:' . $e->getMessage()); //En cas d'erreur, on affiche un message et on arrete tout
}
}
$reponse = $bdd->query('SELECT * FROM myDB'); //récupération de tout le contenu de la table MyDB
// On affiche chaque entrée une à une
while ($donnees = $reponse->fetch())
{
?>
<p>
<strong>Name</strong> : <?php echo $donnees['Place']; ?><br />
</p>
<?php
}
$reponse->closeCursor(); // Termine le traitement de la requête
?>
I've been googling this mistake already but could not find anything relatable. Thank you all for your help.