-1

This will be probably stupid problem, but I can't solve it.

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 I receive this error:

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)

manniL
  • 7,157
  • 7
  • 46
  • 72
Jan Kočvara
  • 81
  • 10

2 Answers2

3

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);
Thomas Lomas
  • 1,553
  • 10
  • 22
2

Your variable of connection is $db, not $bdd