PDO connection to bank

1

Well guys, I'm starting to study about PDO, but soon in my first activity there is an error, all the information is below.

CODE Object.php

<?php

$cli = new Produtos();
$cli->insert();

CODE Product.php

<?php

class Produtos extends Conexao{

    public function insert(){

        $this->conectar();

    }
}

CODE Connection.php

class Conexao{

    private static $conexao;

        public function conectar(){
            try{

                if (!isset(self::$conexao)) {
                    self::$Conexao = new pdo("mysql:host=localhost; dbname = renanmeh_bd_projeto","root","")
                }

            }catch(PDOException $e){
                echo "Erro ao conectar ao banco ".$e->getMessage;
            }
            return self::$conexao
        }
    }
Basically this is just a test, where I have object that instantiates the product class, and this product class makes a request in the connection class.

    
asked by anonymous 04.06.2015 / 05:46

1 answer

5

Your code has a number of minor errors:

In the line where the PDO is instantiated is missing a semicolon and variables, etc. properties are case sensitive ie case-sensitive make the property name defined at the beginning is $conexao and not $Conexao (as seen in assignment).

No echo within the catch getMessage () is a method as soon as the use of parentheses is required.

No return also spoke a semicolon.

class Conexao{
   private static $conexao;
   public function conectar(){
        try{
            if (!isset(self::$conexao)) {
                //Não é $Conexao
                self::$conexao = new pdo("mysql:host=localhost; dbname=renanmeh_bd_projeto","root",""); //;
            }

        }catch(PDOException $e){
            echo "Erro ao conectar ao banco ".$e->getMessage(); //método
        }
        return self::$conexao; //;
    }
}

Recommended reading:

functions and methods in PHP are case-insensitive?

    
04.06.2015 / 06:29