Error using PDO prepare () function [closed]

1

I'm in a new project, and I need to use prepare() of the PDO, I do not know why, but only with it that is giving this error, every time it has some function, or some line with prepare() , PHP returns an error.

I have already searched the entire OS, I found answers, but I tried them all and no question was the same as mine / or it was similar, but the answer did not resolve.

Page code conexao.php :

$pdo = new PDO("mysql:host=localhost;dbname=central", "root", "");

Page code index.php :

require_once('conexao.php');
$sql = $pdo->prepare("INSERT INTO logs VALUES (:id, :type, :info)");
$sql->execute(array(":id" => NULL, ":type" => $type, ":info" => $info));

Informed error:

Notice: Undefined variable: pdo in C:\xampp\htdocs\index.php on line 2

Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs\index.php on line 2
    
asked by anonymous 17.11.2016 / 07:48

1 answer

4

It would be nice if you post the whole code for better clarification. Either way, try hone the code for the connection.

<?php
if(!defined("HOST")){ define('HOST','localhost'); }
if(!defined("DATABASE")){ define('DATABASE','central');}
if(!defined("USER")){ define('USER','root'); }
if(!defined("PASS")){ define('PASS',''); }

$conexao = 'mysql:host='.HOST.';dbname='.DATABASE;

try{
    $pdo = new PDO($conexao, USER, PASS);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOexception $e){
    echo "Erro ao conectar" . $e->getMessage();
}
?>

I hope you have helped. A hug

    
17.11.2016 / 12:32