Fatal error: Uncaught Error: Call to a member function prepare()
Fatal error: Uncaught Error: Call to a member function prepare()
我想在用户中添加信息session。我从数据库中获取这些数据,但我总是遇到这个错误:
Fatal error: Uncaught Error: Call to a member function prepare() on null in /web/htdocs/-----/home/classes/functions.php:10 Stack trace: #0 /web/htdocs/------/home/classes/functions.php(25): Functions->getInformazioni(11) #1 /web/htdocs/------/home/profile.php(11): Functions->dropInformazioni(11) #2 {main} thrown in /web/htdocs/---------/home/classes/functions.php on line 10
我用“-----”替换我的网站。
这是我的代码:
profile.php
<?php
require(__DIR__.'/includes/config.php');
$title = 'F&S: Profilo';
require(__DIR__.'/layout/head.php');
//if not logged in redirect to login page
if (! $user->is_logged_in()){
header('Location: index.php');
exit();
}
$function->dropInformazioni($_SESSION['memberID']);
//include header template
require(__DIR__.'/layout/header.php');
?>
<div class="container">
<div class="main-body">
<div class="row gutters-sm">
<div class="col-md-4 mb-3">
<div class="card">
<div class="card-body">
<div class="d-flex flex-column align-items-center text-center">
<img class="img-res" src="https://avatar.tobi.sh/<?php echo $_SESSION['email'];?>.svg?text=<?php echo (substr($_SESSION['name'], 0, 1)); echo (substr($_SESSION['surname'], 0, 1));?>" width="150">
<div class="mt-3">
<h4><?php echo $_SESSION['name']; ?> <?php echo $_SESSION['surname']; ?></h4>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-8">
<div class="card mb-3">
<div class="card-body">
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Full Name</h6>
</div>
<div class="col-sm-9 text-secondary">
Kenneth Valdez
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Email</h6>
</div>
<div class="col-sm-9 text-secondary">
fip@jukmuh.al
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Phone</h6>
</div>
<div class="col-sm-9 text-secondary">
(239) 816-9029
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Mobile</h6>
</div>
<div class="col-sm-9 text-secondary">
(320) 380-4539
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Address</h6>
</div>
<div class="col-sm-9 text-secondary">
Bay Area, San Francisco, CA
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-12">
<a class="btn btn-info " href="#">Edit</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
require(__DIR__.'/layout/footer.php');
?>
functions.php的一部分
private function getInformazioni($id) {
try {
$stmt = $this->$_db->prepare('SELECT memberID, name, surname, comune, provincia, via, prefisso_int, telefono FROM members WHERE memberID = '.$id.' AND active="Yes"');
$stmt->execute(array('memberID' => $id));
return $stmt->fetch();
} catch (PDOException $e) {
echo "$e";
}
}
public function dropInformazioni($id) {
$row = $this->getInformazioni($id);
$_SESSION['name'] = $row['name'];
$_SESSION['surname'] = $row['surname'];
}
如果我将 classes 中的代码放入 profile.php 脚本同时工作 如果我使用 php class 它会给我错误在标题中。我希望你能明白我说的话。
抱歉我的英语不好。
您不小心使用了 variable variable。
查看此演示:
<?php
class myclass {
public $foo = 'foo';
public $bar = 'bar';
function __construct() {
}
function myfunc($foo) {
echo "foo = {$this->foo}\n";
echo "bar = {$this->bar}\n";
echo "variable '$foo' = {$this->$foo}\n";
}
}
$m = new myclass();
$m->myfunc('foo');
$m->myfunc('bar');
$m->myfunc('baz');
输出:
foo = foo
bar = bar
variable 'foo' = foo
foo = foo
bar = bar
variable 'bar' = bar
foo = foo
bar = bar
PHP Warning: Undefined property: myclass::$baz in p.php on line 13
Warning: Undefined property: myclass::$baz in p.php on line 13
variable 'baz' =
结论:当你引用一个class成员变量时,如果你想直接引用该变量,不要使用第二个$
。如果要使用可变变量,只使用第二个 $
。即成员变量的名称在字符串变量中。
我想在用户中添加信息session。我从数据库中获取这些数据,但我总是遇到这个错误:
Fatal error: Uncaught Error: Call to a member function prepare() on null in /web/htdocs/-----/home/classes/functions.php:10 Stack trace: #0 /web/htdocs/------/home/classes/functions.php(25): Functions->getInformazioni(11) #1 /web/htdocs/------/home/profile.php(11): Functions->dropInformazioni(11) #2 {main} thrown in /web/htdocs/---------/home/classes/functions.php on line 10
我用“-----”替换我的网站。
这是我的代码: profile.php
<?php
require(__DIR__.'/includes/config.php');
$title = 'F&S: Profilo';
require(__DIR__.'/layout/head.php');
//if not logged in redirect to login page
if (! $user->is_logged_in()){
header('Location: index.php');
exit();
}
$function->dropInformazioni($_SESSION['memberID']);
//include header template
require(__DIR__.'/layout/header.php');
?>
<div class="container">
<div class="main-body">
<div class="row gutters-sm">
<div class="col-md-4 mb-3">
<div class="card">
<div class="card-body">
<div class="d-flex flex-column align-items-center text-center">
<img class="img-res" src="https://avatar.tobi.sh/<?php echo $_SESSION['email'];?>.svg?text=<?php echo (substr($_SESSION['name'], 0, 1)); echo (substr($_SESSION['surname'], 0, 1));?>" width="150">
<div class="mt-3">
<h4><?php echo $_SESSION['name']; ?> <?php echo $_SESSION['surname']; ?></h4>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-8">
<div class="card mb-3">
<div class="card-body">
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Full Name</h6>
</div>
<div class="col-sm-9 text-secondary">
Kenneth Valdez
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Email</h6>
</div>
<div class="col-sm-9 text-secondary">
fip@jukmuh.al
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Phone</h6>
</div>
<div class="col-sm-9 text-secondary">
(239) 816-9029
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Mobile</h6>
</div>
<div class="col-sm-9 text-secondary">
(320) 380-4539
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Address</h6>
</div>
<div class="col-sm-9 text-secondary">
Bay Area, San Francisco, CA
</div>
</div>
<hr>
<div class="row">
<div class="col-sm-12">
<a class="btn btn-info " href="#">Edit</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
require(__DIR__.'/layout/footer.php');
?>
functions.php的一部分
private function getInformazioni($id) {
try {
$stmt = $this->$_db->prepare('SELECT memberID, name, surname, comune, provincia, via, prefisso_int, telefono FROM members WHERE memberID = '.$id.' AND active="Yes"');
$stmt->execute(array('memberID' => $id));
return $stmt->fetch();
} catch (PDOException $e) {
echo "$e";
}
}
public function dropInformazioni($id) {
$row = $this->getInformazioni($id);
$_SESSION['name'] = $row['name'];
$_SESSION['surname'] = $row['surname'];
}
如果我将 classes 中的代码放入 profile.php 脚本同时工作 如果我使用 php class 它会给我错误在标题中。我希望你能明白我说的话。 抱歉我的英语不好。
您不小心使用了 variable variable。
查看此演示:
<?php
class myclass {
public $foo = 'foo';
public $bar = 'bar';
function __construct() {
}
function myfunc($foo) {
echo "foo = {$this->foo}\n";
echo "bar = {$this->bar}\n";
echo "variable '$foo' = {$this->$foo}\n";
}
}
$m = new myclass();
$m->myfunc('foo');
$m->myfunc('bar');
$m->myfunc('baz');
输出:
foo = foo
bar = bar
variable 'foo' = foo
foo = foo
bar = bar
variable 'bar' = bar
foo = foo
bar = bar
PHP Warning: Undefined property: myclass::$baz in p.php on line 13
Warning: Undefined property: myclass::$baz in p.php on line 13
variable 'baz' =
结论:当你引用一个class成员变量时,如果你想直接引用该变量,不要使用第二个$
。如果要使用可变变量,只使用第二个 $
。即成员变量的名称在字符串变量中。