ns am make the efforts to access a perform of categories and their contents. I have actually a course called Categories. I keep getting this error. The weird point is that I"ve offered this same exact code in 2 other places so far with no problems. All ns did here was reuse the code and readjust all the variables.

You are watching: Call to a member function prepare() on null

Fatal error: speak to to a member role prepare() on nullHere is the code to my class:

prepare("SELECT * native dd_cat"); $query->execute(); return $query->fetchAll(); } public function fetch_data($cat_id) worldwide $pdo; $query = $pdo->prepare("SELECT * from dd_cat where cat_id = ?"); $query->bindValue(1, $cat_id); $query->execute(); return $query->fetch(); }?>And below is the code I am trying to call:

is_logged_in() ) echo "Hello " . $_SESSION<"user_name"> . ". No you? sign out"; rather echo "Sign in or develop an account."; ?>


It looks like your $pdo change is no initialized.I can"t check out in the password you"ve uploaded whereby you room initializing it.

Make certain you produce a new PDO object in the an international scope before calling the course methods. (You should declare the in the worldwide scope due to the fact that of how you imposed the techniques inside the classification class).

$pdo = brand-new PDO("mysql:host=localhost;dbname=test", $user, $pass);



You deserve to try/catch PDOExceptions (your configs can differ yet the important component is the try/catch):

try $dbh = brand-new PDO( DB_TYPE . ":host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET, DB_USER, DB_PASS, < PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET names " . DB_CHARSET . " COLLATE " . DB_COLLATE > ); catch ( PDOException $e ) echo "ERROR!"; print_r( $e ); The print_r( $e ); line will show you every little thing you need, for example I had a recent situation where the error post was prefer unknown database "my_db".


delato468 comment should be noted as a solution as it operated for me.

In addition to specifying the parameter, the user should pass it as well at the moment of phone call the function

fetch_data(PDO $pdo, $cat_id)


I additionally faced very same issue.for me the if condition prior to the try catch was not correct.it have to be

public static role getdb() if(!isset(self::db)) shot record (PDOException $e) $msg = $e->getMessage(); echo $msg; exit(); return self::db;
In ---- model:Add use JenssegersMongodbEloquentModel as Eloquent;

Change the course ----- extends model to course ----- extends Eloquent

Thanks for contributing response to ridge Overflow!

Please be certain to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; earlier them increase with referrals or an individual experience.

See more: Bicycle Architectural Wonders Of The World, Bicycle ® Architectural Wonders Of The World

To discover more, check out our tips on writing an excellent answers.

write-up Your prize Discard

By clicking “Post her Answer”, friend agree come our terms of service, privacy policy and also cookie policy

update profile picture but kept obtaining this deadly error: Uncaught Error: speak to to a member function prepare() top top null
site design / logo design © 2021 ridge Exchange Inc; user contributions licensed under cc by-sa. Rev2021.11.1.40614

her privacy

By click “Accept every cookies”, girlfriend agree stack Exchange have the right to store cookie on your maker and disclose information in accordance with our Cookie Policy.