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 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信