1

This code gets an error.

Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\progapps\addReminder.php on line 12

Here is my code:

addReminder.php

<?php

include_once("includes/database.php");

try {
    global $dbh;
    $query = $dbh -> prepare("SELECT * FROM reminder_type;");
    $query->setFetchMode(PDO::FETCH_ASSOC);
    $query ->execute();
} catch(PDOException $ex) {
    echo $ex->getMessage();
}

?>

This is the connection of the database.

database.php

<?php

    include("constants.php");

    class MySQLDB { 

       function MySQLDB(){
           global $dbh;
           try{    
               $dbh = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME.'',DB_USER,DB_PASS);
               $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
               $dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
           }catch(PDOException $e){
               echo $e->getMessage();
               die();
           }
        }
     }

Here are the constants

constants.php

<?php
$currency = '&#x20b1;';
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "hrisdb");
?>

What is the possible cause? And how can I solve this problem? Thank you, guys!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
krb.js
  • 37
  • 10

2 Answers2

1

This should work

$currency = '&#x20b1;';
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "hrisdb");



try {
    $dbh = new PDO('mysql:host=' . DB_SERVER . ';dbname=' . DB_NAME . '', DB_USER, DB_PASS);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
} catch (PDOException $e) {
    echo $e->getMessage();
    die();
}




try {

    $query = $dbh->prepare("SELECT * FROM reminder_type;");
    $query->setFetchMode(PDO::FETCH_ASSOC);
    $query->execute();
} catch (PDOException $ex) {
    echo $ex->getMessage();
}

if you want it inside a class take a look at this extending PDO class

Community
  • 1
  • 1
Jelle Keizer
  • 723
  • 5
  • 9
  • Works fine here, what error do you get ? – Jelle Keizer Dec 05 '14 at 16:20
  • Did you remove all the class stuff you did in database.php? Copy this code in a file and it works. – Jelle Keizer Dec 05 '14 at 16:23
  • It works now, man. Thanks. Why am I getting that error? – krb.js Dec 05 '14 at 16:26
  • 1
    @JonKirbyJosue Remember to ***upvote*** and ***accept (click the tick)*** if you feel the answer has answered your question. – AStopher Dec 05 '14 at 16:28
  • You made some class MySQLDB but never created a new instance, but i figured you probably didnt know what you where doing and the class had no use anyway so took the code outside the class. i suggest you read an article like this http://codular.com/introducing-php-classes – Jelle Keizer Dec 05 '14 at 16:31
0

You haven't called the function MySQLDB. So the variable $dbh doesn't even exist.

Flosculus
  • 6,880
  • 3
  • 18
  • 42