我目前正在使用OOP编写留言簿php练习插件,但遗憾的是我遇到了一个问题,即我的pdo连接返回null。令人难以理解的是,连接类存储在同一属性中的工作没有任何问题,只有几行之前。然而,显然,我似乎没有找到它不起作用的原因...... :(
这是db类:
class db_sql
{
protected $host = 'localhost',
$user = 'root',
$pw = '',
$dbname = 'gaestebuch';
public $initialCn;
static public $db_obj = null;
static public $cn;
public function __construct ()
{
$this->initialCn = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->dbname, $this->user, $this->pw);
$this->initialCn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
public static function getInstance ()
{
if(self::$db_obj === null)
self::$cn = new db_sql;
return self::$db_obj;
}
public final function __clone () { }
}
这是用户类(仅用于继承)
class User extends db_sql
{
protected $name = null,
$email = null,
$msg = null;
public function __construct($name = 'name', $email = 'email', $msg = 'msg')
{
$this->name = $name;
$this->email = $email;
$this->msg = $msg;
}
public function getName(){
return $this->name;
}
public function getEmail(){
return $this->email;
}
public function getMsg(){
return $this->msg;
}
}
这是帖子类:
class Eintrag extends User
{
public $listed_at = null;
public static $list_decay = false,
$interaction = null;
protected static $pdo;
private static $error = false;
public function __construct($pdo = null)
{
self::$pdo = new db_sql();
}
public static function setEintrag($user)
{
$name = $user->getName();
$email = $user->getEmail();
$msg = $user->getMsg();
if(!empty($name) && !empty($email) && !empty($msg))
{
if(!self::$pdo == null)
{
$sql = 'INSERT INTO gaestebuch(name, email, text) VALUES(:name, :email, :text)';
$stmt = self::$pdo->initialCn->prepare($sql);
$stmt->execute(['name' => $name, 'email' => $email, 'text' => $msg]);
return true;
} else {
self::$error = "Ein Fehler bezüglich der Datenbankverbindung ist aufgetreten.";
self::errorListener();
return false;
}
} else {
self::$error = "Bitte füllen sie alle Felder aus.";
self::errorListener();
return false;
}
}
public static function showPosts()
{
if(!self::$pdo == null)
{
$sql = 'SELECT * FROM gaestebuch';
// $pdo in line 48 returns null
$stmt = self::$pdo->initialCn->prepare($sql);
$stmt->execute(['name' => $name, 'email' => $email, 'text' => $text, 'created_at' => $created_at]);
$posts = $stmt->fetchAll();
foreach($posts as $post)
{
echo $post->name . '( ' . $post->email . ' ) schrieb am ' . $post->created_at . ' folgendes:<br>';
echo $post->text . '<br>';
echo '<hr>';
echo '<br>';
}
return true;
} else {
self::$error = "Ein Fehler bezüglich der Datenbankverbindung ist aufgetreten.";
self::errorListener();
return false;
}
}
public static function errorListener()
{
if(!self::$error == false){
echo self::$error;
return true;
}
}
}
这是主要的脚本:
require_once(dirname(__DIR__).'/vars.php');
if(isset($_GET['eintrag']))
{
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = trim($_POST['msg']);
if(!filter_var($email, FILTER_VALIDATE_EMAIL) === false && !empty($name) && !empty($msg))
{
$user = new User($name, $email, $msg);
$userEintrag = new Eintrag();
$userEintrag->setEintrag($user);
}else{
$error = "Bitte füllen sie alle Felder richtig aus.";
echo $error;
}
}
最后是我的vars.php
define('gstl_name', 'Gaesteliste');
define('gstl_version', '1.6.3');
define('gstl_dir', dirname(__FILE__).'/');
require_once(gstl_dir.'/misc/db.php');
require_once(gstl_dir.'/misc/user.class.php');
require_once(gstl_dir.'/misc/eintrag.class.php');
require_once(gstl_dir.'/main/script.php');
有人知道什么是错误的和/或甚至有办法解决这个问题吗?提前感谢您的关注