I'm new to the PDO and I'm seeing some video lessons and also taking a look at the theoretical part, but when I try to make a insert
in DB, I'm returning 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 into the doc itself that php.net has about and I can not solve it.