XxGhostCZ XxGhostCZ - 7 months ago 19
SQL Question

"prepare" PDO function error

this will be probably stupid problem, but i can't solve it, so i hoped, you can help me guys.

I have PDO connection (bdd.php)

<?php
$dbserver="localhost";
$dbuser="root";
$dbpass="";
$dbname="calendar";

$db = new PDO(
"mysql:host=$dbserver;dbname=$dbname;" ,"$dbuser","$dbpass",
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8"
)
);
?>


But an error is here ->


Fatal error: Call to a member function prepare() on a non-object on Index.php on row 6


Here is my "Index.php" :

<?php
require_once('bdd.php');

$sql = "SELECT id, title, start, end, color FROM events ";

$req = $bdd->prepare($sql);
$req->execute();

$events = $req->fetchAll();

?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">


..... (continues is normal HTML / PHP / JS content, not important)

Thank you for you help!

Answer

It looks like $req = $bdd->prepare($sql); is where your problem is coming from.

Inside bdd.php you are assigning the database connection to $db.

Simply change either of them so that they are the same.

So either:

$bdd = new PDO(

Or:

$req = $db->prepare($sql);
Comments