PDO 'Call to a member function prepare() on array'



  • Galera, I'm new to PDO and I'm seeing some video lessons and also taking a look at the theoric part, but while trying to perform a insert in the DB, is returning me the following error Fatal error: Call to a member function prepare() on array in C:\FullProg\www\Thomas\prog\assets\inc\creat.php on line 19.

    The code is below:

    <?php 
    // include_once 'conexao.php';
    function getConnection(){
        $host = "localhost";
        $user = "root";
        $pass = "";
        $db_name = "pdo";
        try{
            $conecta = new PDO("mysql:host=".$host.";dbname=".$db_name, $user, $pass);
            return array("conexao" => $conecta, "mensagem" => "Sucesso");
        } catch(PDOException $e){
            return array("conexao" => null, "mensagem" => "Algo de errado não está certo. <br> Erro: " . $e -> getMessage());
        }
    }
    $conecta = getConnection();
    $tipo = "Tipo";
    $tamanho = "1M";
    $nome = "Prod";
    // $sql = "INSERT INTO produtos (nome, tamanho, tipo) VALUES (:nome, :tamanho, :tipo)";
    // $stmt = $conecta->prepare($sql);
    $stmt = $conecta->prepare("INSERT INTO produtos (nome, tamanho, tipo) VALUES (:nome, :tamanho, :tipo)");
    $stmt->bindParam( ':nome', $nome );
    $stmt->bindParam( ':tamanho', $site );
    $stmt->bindParam( ':tipo', $tipo );
    if ($stmt->execute()) {
        echo "Dados Salvos " . $nome;
    }else{
        echo "Ocoreu um erro " . $nome;
    }
    

    ?>

    I've already searched in the doc itself that php.net has about and I can't fix it.



  • Change this line:

    $stmt->bindParam( ':tamanho', $site );
    

    by this:

    $stmt->bindParam( ':tamanho', $tamanho);
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2