Fatal error: Call to a member function prepare() on null
I am trying to access a list of categories and their contents. I have a class called Categories. I keep getting this error. The weird thing is that I've used this same exact code in two other places so far with no problems. All I did here was reuse the code and change all the variables.
Fatal error: Call to a member function prepare() on null
Here is the code to my class:
<?php
class Category {
public function fetch_all() {
global $pdo;
$query = $pdo->prepare("SELECT * FROM dd_cat");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($cat_id) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM dd_cat WHERE cat_id = ?");
$query->bindValue(1, $cat_id);
$query->execute();
return $query->fetch();
}
}
?>
And here is the code I am trying to call:
<?php
session_start();
//Add session_start to top of each page//
require_once('includes/config.php');
require_once('includes/header.php');
include_once('includes/category.php');
?>
<link rel="stylesheet" href="css/dd.css">
<div id="menu">
<a class="item" href="drop_index.php">Home</a> -
<a class="item" href="create_topic.php">Create a topic</a> -
<a class="item" href="create_cat.php">Create a category</a>
<div id="userbar">
<?php
if( $user->is_logged_in() )
{
echo 'Hello ' . $_SESSION['user_name'] . '. Not you? <a href="logout.php">Sign out</a>';
}
else
{
echo '<a href="login.php">Sign in</a> or <a href="index.php">create an account</a>.';
}
?>
</div>
</div>
<?php
$category = new Category;
$categories = $category->fetch_all();
?>
<div id ="wrapper">
<h1>Categories</h1>
<section>
<ul>
<?php foreach ($categories as $category) { ?>
<li><a href="category.php?id=<?php echo $category['cat_id']; ?>">
<?php echo $category['cat_title']; ?></a>
</li>
<?php } ?>
</ul>
</section>
</div>
<?php
require_once('includes/footer.php');
?>
php
add a comment |
I am trying to access a list of categories and their contents. I have a class called Categories. I keep getting this error. The weird thing is that I've used this same exact code in two other places so far with no problems. All I did here was reuse the code and change all the variables.
Fatal error: Call to a member function prepare() on null
Here is the code to my class:
<?php
class Category {
public function fetch_all() {
global $pdo;
$query = $pdo->prepare("SELECT * FROM dd_cat");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($cat_id) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM dd_cat WHERE cat_id = ?");
$query->bindValue(1, $cat_id);
$query->execute();
return $query->fetch();
}
}
?>
And here is the code I am trying to call:
<?php
session_start();
//Add session_start to top of each page//
require_once('includes/config.php');
require_once('includes/header.php');
include_once('includes/category.php');
?>
<link rel="stylesheet" href="css/dd.css">
<div id="menu">
<a class="item" href="drop_index.php">Home</a> -
<a class="item" href="create_topic.php">Create a topic</a> -
<a class="item" href="create_cat.php">Create a category</a>
<div id="userbar">
<?php
if( $user->is_logged_in() )
{
echo 'Hello ' . $_SESSION['user_name'] . '. Not you? <a href="logout.php">Sign out</a>';
}
else
{
echo '<a href="login.php">Sign in</a> or <a href="index.php">create an account</a>.';
}
?>
</div>
</div>
<?php
$category = new Category;
$categories = $category->fetch_all();
?>
<div id ="wrapper">
<h1>Categories</h1>
<section>
<ul>
<?php foreach ($categories as $category) { ?>
<li><a href="category.php?id=<?php echo $category['cat_id']; ?>">
<?php echo $category['cat_title']; ?></a>
</li>
<?php } ?>
</ul>
</section>
</div>
<?php
require_once('includes/footer.php');
?>
php
2
var_dump($pdo)
,$pdo
is not a object(not initialized yet) whenprepare
is called.
– Jigar
Feb 18 '15 at 19:41
1
@JandenHale: did you initialize a (global) variable with the name$pdo
before that?
– Willem Van Onsem
Feb 18 '15 at 20:09
It's global in the class, at least I thought it was.
– ddonche
Feb 18 '15 at 20:40
add a comment |
I am trying to access a list of categories and their contents. I have a class called Categories. I keep getting this error. The weird thing is that I've used this same exact code in two other places so far with no problems. All I did here was reuse the code and change all the variables.
Fatal error: Call to a member function prepare() on null
Here is the code to my class:
<?php
class Category {
public function fetch_all() {
global $pdo;
$query = $pdo->prepare("SELECT * FROM dd_cat");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($cat_id) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM dd_cat WHERE cat_id = ?");
$query->bindValue(1, $cat_id);
$query->execute();
return $query->fetch();
}
}
?>
And here is the code I am trying to call:
<?php
session_start();
//Add session_start to top of each page//
require_once('includes/config.php');
require_once('includes/header.php');
include_once('includes/category.php');
?>
<link rel="stylesheet" href="css/dd.css">
<div id="menu">
<a class="item" href="drop_index.php">Home</a> -
<a class="item" href="create_topic.php">Create a topic</a> -
<a class="item" href="create_cat.php">Create a category</a>
<div id="userbar">
<?php
if( $user->is_logged_in() )
{
echo 'Hello ' . $_SESSION['user_name'] . '. Not you? <a href="logout.php">Sign out</a>';
}
else
{
echo '<a href="login.php">Sign in</a> or <a href="index.php">create an account</a>.';
}
?>
</div>
</div>
<?php
$category = new Category;
$categories = $category->fetch_all();
?>
<div id ="wrapper">
<h1>Categories</h1>
<section>
<ul>
<?php foreach ($categories as $category) { ?>
<li><a href="category.php?id=<?php echo $category['cat_id']; ?>">
<?php echo $category['cat_title']; ?></a>
</li>
<?php } ?>
</ul>
</section>
</div>
<?php
require_once('includes/footer.php');
?>
php
I am trying to access a list of categories and their contents. I have a class called Categories. I keep getting this error. The weird thing is that I've used this same exact code in two other places so far with no problems. All I did here was reuse the code and change all the variables.
Fatal error: Call to a member function prepare() on null
Here is the code to my class:
<?php
class Category {
public function fetch_all() {
global $pdo;
$query = $pdo->prepare("SELECT * FROM dd_cat");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($cat_id) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM dd_cat WHERE cat_id = ?");
$query->bindValue(1, $cat_id);
$query->execute();
return $query->fetch();
}
}
?>
And here is the code I am trying to call:
<?php
session_start();
//Add session_start to top of each page//
require_once('includes/config.php');
require_once('includes/header.php');
include_once('includes/category.php');
?>
<link rel="stylesheet" href="css/dd.css">
<div id="menu">
<a class="item" href="drop_index.php">Home</a> -
<a class="item" href="create_topic.php">Create a topic</a> -
<a class="item" href="create_cat.php">Create a category</a>
<div id="userbar">
<?php
if( $user->is_logged_in() )
{
echo 'Hello ' . $_SESSION['user_name'] . '. Not you? <a href="logout.php">Sign out</a>';
}
else
{
echo '<a href="login.php">Sign in</a> or <a href="index.php">create an account</a>.';
}
?>
</div>
</div>
<?php
$category = new Category;
$categories = $category->fetch_all();
?>
<div id ="wrapper">
<h1>Categories</h1>
<section>
<ul>
<?php foreach ($categories as $category) { ?>
<li><a href="category.php?id=<?php echo $category['cat_id']; ?>">
<?php echo $category['cat_title']; ?></a>
</li>
<?php } ?>
</ul>
</section>
</div>
<?php
require_once('includes/footer.php');
?>
php
php
asked Feb 18 '15 at 19:39
ddonche
4491520
4491520
2
var_dump($pdo)
,$pdo
is not a object(not initialized yet) whenprepare
is called.
– Jigar
Feb 18 '15 at 19:41
1
@JandenHale: did you initialize a (global) variable with the name$pdo
before that?
– Willem Van Onsem
Feb 18 '15 at 20:09
It's global in the class, at least I thought it was.
– ddonche
Feb 18 '15 at 20:40
add a comment |
2
var_dump($pdo)
,$pdo
is not a object(not initialized yet) whenprepare
is called.
– Jigar
Feb 18 '15 at 19:41
1
@JandenHale: did you initialize a (global) variable with the name$pdo
before that?
– Willem Van Onsem
Feb 18 '15 at 20:09
It's global in the class, at least I thought it was.
– ddonche
Feb 18 '15 at 20:40
2
2
var_dump($pdo)
, $pdo
is not a object(not initialized yet) when prepare
is called.– Jigar
Feb 18 '15 at 19:41
var_dump($pdo)
, $pdo
is not a object(not initialized yet) when prepare
is called.– Jigar
Feb 18 '15 at 19:41
1
1
@JandenHale: did you initialize a (global) variable with the name
$pdo
before that?– Willem Van Onsem
Feb 18 '15 at 20:09
@JandenHale: did you initialize a (global) variable with the name
$pdo
before that?– Willem Van Onsem
Feb 18 '15 at 20:09
It's global in the class, at least I thought it was.
– ddonche
Feb 18 '15 at 20:40
It's global in the class, at least I thought it was.
– ddonche
Feb 18 '15 at 20:40
add a comment |
2 Answers
2
active
oldest
votes
It looks like your $pdo
variable is not initialized.
I can't see in the code you've uploaded where you are initializing it.
Make sure you create a new PDO object in the global scope
before calling the class methods. (You should declare it in the global scope because of how you implemented the methods inside the Category class).
$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
That's it right there, I have it in my config file as $db instead of $pdo. Thanks!
– ddonche
Feb 18 '15 at 20:42
1
Dont use global ! Add in your function: fetch_data(PDO $pdo, $cat_id)
– delato468
Apr 23 '18 at 16:00
In this case, it's more of a constant.
– ddonche
Nov 1 '18 at 15:59
add a comment |
You can try/catch PDOException
s (your configs could differ but the important part is the try/catch):
try {
$dbh = 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 everything you need, for example I had a recent case where the error message was like unknown database 'my_db'
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28592400%2ffatal-error-call-to-a-member-function-prepare-on-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like your $pdo
variable is not initialized.
I can't see in the code you've uploaded where you are initializing it.
Make sure you create a new PDO object in the global scope
before calling the class methods. (You should declare it in the global scope because of how you implemented the methods inside the Category class).
$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
That's it right there, I have it in my config file as $db instead of $pdo. Thanks!
– ddonche
Feb 18 '15 at 20:42
1
Dont use global ! Add in your function: fetch_data(PDO $pdo, $cat_id)
– delato468
Apr 23 '18 at 16:00
In this case, it's more of a constant.
– ddonche
Nov 1 '18 at 15:59
add a comment |
It looks like your $pdo
variable is not initialized.
I can't see in the code you've uploaded where you are initializing it.
Make sure you create a new PDO object in the global scope
before calling the class methods. (You should declare it in the global scope because of how you implemented the methods inside the Category class).
$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
That's it right there, I have it in my config file as $db instead of $pdo. Thanks!
– ddonche
Feb 18 '15 at 20:42
1
Dont use global ! Add in your function: fetch_data(PDO $pdo, $cat_id)
– delato468
Apr 23 '18 at 16:00
In this case, it's more of a constant.
– ddonche
Nov 1 '18 at 15:59
add a comment |
It looks like your $pdo
variable is not initialized.
I can't see in the code you've uploaded where you are initializing it.
Make sure you create a new PDO object in the global scope
before calling the class methods. (You should declare it in the global scope because of how you implemented the methods inside the Category class).
$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
It looks like your $pdo
variable is not initialized.
I can't see in the code you've uploaded where you are initializing it.
Make sure you create a new PDO object in the global scope
before calling the class methods. (You should declare it in the global scope because of how you implemented the methods inside the Category class).
$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
edited Sep 28 '18 at 13:19
Funk Forty Niner
80.5k1247101
80.5k1247101
answered Feb 18 '15 at 20:18
Dan Revah
2,95023155
2,95023155
That's it right there, I have it in my config file as $db instead of $pdo. Thanks!
– ddonche
Feb 18 '15 at 20:42
1
Dont use global ! Add in your function: fetch_data(PDO $pdo, $cat_id)
– delato468
Apr 23 '18 at 16:00
In this case, it's more of a constant.
– ddonche
Nov 1 '18 at 15:59
add a comment |
That's it right there, I have it in my config file as $db instead of $pdo. Thanks!
– ddonche
Feb 18 '15 at 20:42
1
Dont use global ! Add in your function: fetch_data(PDO $pdo, $cat_id)
– delato468
Apr 23 '18 at 16:00
In this case, it's more of a constant.
– ddonche
Nov 1 '18 at 15:59
That's it right there, I have it in my config file as $db instead of $pdo. Thanks!
– ddonche
Feb 18 '15 at 20:42
That's it right there, I have it in my config file as $db instead of $pdo. Thanks!
– ddonche
Feb 18 '15 at 20:42
1
1
Dont use global ! Add in your function: fetch_data(PDO $pdo, $cat_id)
– delato468
Apr 23 '18 at 16:00
Dont use global ! Add in your function: fetch_data(PDO $pdo, $cat_id)
– delato468
Apr 23 '18 at 16:00
In this case, it's more of a constant.
– ddonche
Nov 1 '18 at 15:59
In this case, it's more of a constant.
– ddonche
Nov 1 '18 at 15:59
add a comment |
You can try/catch PDOException
s (your configs could differ but the important part is the try/catch):
try {
$dbh = 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 everything you need, for example I had a recent case where the error message was like unknown database 'my_db'
.
add a comment |
You can try/catch PDOException
s (your configs could differ but the important part is the try/catch):
try {
$dbh = 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 everything you need, for example I had a recent case where the error message was like unknown database 'my_db'
.
add a comment |
You can try/catch PDOException
s (your configs could differ but the important part is the try/catch):
try {
$dbh = 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 everything you need, for example I had a recent case where the error message was like unknown database 'my_db'
.
You can try/catch PDOException
s (your configs could differ but the important part is the try/catch):
try {
$dbh = 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 everything you need, for example I had a recent case where the error message was like unknown database 'my_db'
.
answered Mar 29 '16 at 14:49
aesede
3,91922629
3,91922629
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure 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; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure 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; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28592400%2ffatal-error-call-to-a-member-function-prepare-on-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
var_dump($pdo)
,$pdo
is not a object(not initialized yet) whenprepare
is called.– Jigar
Feb 18 '15 at 19:41
1
@JandenHale: did you initialize a (global) variable with the name
$pdo
before that?– Willem Van Onsem
Feb 18 '15 at 20:09
It's global in the class, at least I thought it was.
– ddonche
Feb 18 '15 at 20:40