duanhuancong1969 2014-12-16 01:18
浏览 43
已采纳

在简单的php页面中无法多次使用自定义PDO类

I coded a custom Database.class.php class that encapsulates a PDO object to interact with a MySQL database.

<?php

require_once('./classes/configs/Config.php');
class Database
{
    private static $instance = null; 

    private  function __construct()
    {}

    public static function getInstance()
    {
        if (self::$instance == null)
        {
            self::$instance = new PDO(
                "mysql:host=".Config::DB_HOST.";dbname=".Config::DB_NAME."", 
                Config::DB_USER, 
                Config::DB_PWD);

            return self::$instance; 
        }
    }
}
?>

I've also coded a custom UserDAO.class.php which allows me for an easier way of performing CRUD operations. For example, when I want to find a user in my database, I procede like so: $userdao->find('tim') and store my findings in a custom user.class.php object. (This class obviously calls a static Database.class.php object to use the PDO object in that class). Here is the find function in my UserDAO.class.php

public static function find($username)
    {
        $db = Database::getInstance();

        $pstmt = $db->prepare("SELECT * FROM users WHERE username = :x");
        $pstmt->execute(array(':x' => $username));

        $result = $pstmt->fetch(PDO::FETCH_OBJ);
        $p = new User();

        if ($result)
        {

            $p->setUser_id($result->user_id);
            $p->setUsername($result->username);
                        $p->setFirst_name($result->first_name);
                        $p->setLast_name($result->last_name);
                        $p->setEmail($result->email);
            $p->setRegistration_date($result->registration_date);
            $pstmt->closeCursor();
            return $p;
        }
        $pstmt->closeCursor();
        return null;
    }

The problem is that I seem to be unable to use the $userdao object more than once on a simple php page.

$userdao = new UserDAO();
$user5=$userdao->find('tim');
echo"<h3>".$user5."</h3>";  // this works fine (I have a toString function)
$user6=$userdao->find("john"); // this does not work....rest of the page is blank!!!!
echo"<h3>".$user6."</h3>";  
  • 写回答

1条回答 默认 最新

  • ds261634878 2014-12-16 01:22
    关注

    You have error here - you return self::$instance only if self::$instance == null

    public static function getInstance() {
        if (self::$instance == null)
        {
            self::$instance = new PDO(
                "mysql:host=".Config::DB_HOST.";dbname=".Config::DB_NAME."", 
                Config::DB_USER, 
                Config::DB_PWD);
            // return self::$instance; // moved down
    
        }
        return self::$instance; // this is the fix
    }
    

    also you could eaisly debug it by yourself the reason why rest of the page is blank!!!!" is because you have disabled error reporting. change php.ini or use error_reporting() to show PHP errors and warnings

    See:

    the reason why page is blank is because PHP is throwing fatal error (you are trying to use method find on null), but since you have no error reporting nothing shows up

    there is no way to develop applications without error reporting, you must always have it enabled in development environment

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果