PDO 'Call to a Member Function prepare() on array'

Asked

Viewed 257 times

1

Guys, I’m new to PDO and I’m watching some video lessons and also taking a look at the theoretical part, but when trying to accomplish a insert in 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 looked in the doc itself that php.net has on and I can’t solve it.

1 answer

0


Change that line:

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

for that:

$stmt->bindParam( ':tamanho', $tamanho);
  • kkkkkkk can’t believe I didn’t see there was no more $site. vlw bro

Browser other questions tagged

You are not signed in. Login or sign up in order to post.