/    Sign up×
Community /Pin to ProfileBookmark

Fatal error: Call to a member function prepare() on null in on line 28

Hi All,
This is my first project on PHP and first time encounter this bug which really can’t solve after struggle one day.
Can any expert and kind soul here advise how to resolve this error please? My PHP version is 5.6.

Thank you so much,

After I submit my form, it shows below error

Fatal error: Call to a member function prepare() on null in on line 28

My DB connection coding is

[code]
<?php
$DSN= ‘mysql:host = localhost; dbname=cms4.2.1’;
$ConnectingDB = new PDO($DSN, ‘root’, ”);

?>
[/code]

Category page that shows line 28 got error on the POD function “prepare”

[code]
if(empty($Category))
{
$_SESSION[“ErrorMessage”] = “All fields must be filled out”;
Redirect_to(“Categories.php”);
} elseif (strlen($Category)<3) {
$_SESSION[“ErrorMessage”] = “Category Title should be greater than 3 characters”;
Redirect_to(“Categories.php”);
} elseif (strlen($Category)>49) {
$_SESSION[“ErrorMessage”] = “Category Title should be less than 50 characters”;
Redirect_to(“Categories.php”);
} else {
global $ConnectingDB;
$sql = “INSERT INTO category(title,author,datetime)”;
$sql .=”VALUES(:categoryName,:adminName,:dateTime)”;
$stmt = $ConnectingDB->prepare($sql); // – > means PDO object rotation – Line 28
$stmt->bindValue(‘:categoryName’,$Category);
$stmt->bindValue(‘:adminName’,$Admin);
$stmt->bindValue(‘:dateTime’,$DateTime);
$Execute=$stmt->execute();
[/code]

Please help me as I have no one to asks already besides this forum.

Thank you so much.

__Edited to add `[code]…[/code]` tags ~ NogDog__

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 05.2019 — The error is that $ConnectingDB is null. It's hard to tell for sure at this point why, but since the initial assignment via new PDO() would throw an exception if it failed, either that code is not in the scope of the part of the script where you use it, or...?

The fact that you are trying to use global to set it just before it fails is a code smell to me that it's out of scope to this script, never being set at all when this script is run. On top of that, using global is almost always a bad (or at least not good) thing: if you need to use a variable within a function or method, pass it in as a parameter -- don't depend on it to be set/defined correctly by some other part of the application.
Copy linkTweet thisAlerts:
@ginerjmMar 05.2019 — As a newbie perhaps you will allow a suggestion to your chosen coding "style". Sure - JavaScript uses the 'camel-back' naming style and people have to get used to that when confronted with using code snippets. But - PHP does not use that and IMHO users should learn to leave out the caps in normal practice. Using upper and lower case names is only going to bite you in the a.., especially when you get to larger scripts and included modules that you are not familiar with. Sure there are (PHP) coding styles that use it and stress its benefits (?), but personally I only use it for special circumstances where remembering whether to use a cap or not is not a problem, such as constants or a common variable name that I use across the board.

Just my $.02...
×

Success!

Help @Sharon1975 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...