0

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.

1
  • This question is not meeting community guidelines.. Iif you want to connect database with pdo then this link will help you phpdelusions.net/pdo
    – Himanshu
    Oct 9 '18 at 9:34
0

You try following code.


<?php
class DBClass {

    protected $servername = "localhost";
    protected $username = "root";
    protected $password = "anything";

    public function run()
    {
        try {
            $bdd = new PDO('mysql:host='.$this->servername.';dbname=myDB;charset=utf8', $this->username, $this->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
    }
}

$obj = new DBClass();
$obj->run();
-1

remove

class DBClass {

or you have to implement this in OOP way.

https://www.w3schools.com/Php/php_mysql_connect.asp

<?php
    $servername = "localhost";
    $username = "root";
    $password = "123";


    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 table1"); 
        $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 table'); //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

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.