【问题标题】:Continuous Clash with Database Handle and Null与数据库句柄和空值持续冲突
【发布时间】:2017-10-15 20:56:20
【问题描述】:

我觉得我在 PHP 中遗漏了一些关于 OOP 的内容。我已经阅读了许多关于连接到 mysql 的教程,但一直遇到错误,告诉我我的数据库句柄不存在,即使它确实连接了。我有以下代码,并且一切都正确连接,但是当我尝试使用 $connection->close 关闭连接时(我知道 PHP 在脚本完成时关闭连接,但这对我来说也是一个学习练习) ();我收到 $database_handle 未定义的通知,因此 mysqli_close($database_handle) 引用了一个 NULL 变量并返回一个致命错误。

为什么 $connection->connect();工作和 $connection->close();返回一个致命错误?

类:

<?php

class database_connection  {
    var $hostname = 'localhost';
    var $username = 'root';
    var $password = '';
    var $database = 'assistant';
    var $database_handle;

    function connect() 
    {
        $database_handle = mysqli_connect($this->hostname, $this->username, $this->password, $this->database);

        if (!$database_handle) 
        {
            die('Could not connect to database!');
        } 
        else 
        {
            $this->database_handle = $database_handle;
            echo 'Connection established!';
        }
        return $this->database_handle;
    }

    function close() 
    {
        mysqli_close($database_handle);
        echo 'Connection closed!';
    }

}

?>

引用page.php:

<?php

include 'includes/database_classes.php';

$connection = new database_connection();
$connection->connect();
$query = 'SELECT * FROM  `users`';
$result = mysqli_query($connection->database_handle, $query);

if($numrows = mysqli_num_rows($result)) 
{
    echo "<br>";
    echo $numrows;
    echo "<br>";

    while ($row = mysqli_fetch_assoc($result)) 
    {
        $database_username = $row['user_name'];
        $database_password = $row['user_password'];
        echo $database_username;
        echo $database_password;
    }
}

$connection->close();   

?>

【问题讨论】:

  • 因为你已经关闭了page.php中的连接
  • 连接通常不需要关闭,它们会在脚本执行结束时自行关闭
  • 您使用的是什么版本的 PHP?用于变量定义的 var 至少在几个主要版本的 PHP 中都没有使用
  • 嗨 Riggs - 它具体在哪里关闭?我明白你在说什么,因为如果连接关闭,数据库句柄将为空,但是在我调用 $connection->close(); 之前它具体关闭在哪里? ?
  • 我正在使用 php 7。我实际上认为这些应该受到保护,但我正在偷工减料,所以我可以先得到一般的理解,而 var 是我想到的第一件事就是得到它去。

标签: php class oop mysqli


【解决方案1】:

我能否建议您采用这种方法,以便您在脚本上一次只有一个连接

这也修复了您的错误

function close() 
{
    mysqli_close($database_handle); // $database_handle does not exist
    echo 'Connection closed!';
}

function close() 
{
    mysqli_close($this->database_handle);
    echo 'Connection closed!';
}

这可以确保即使您在脚本中多次调用 database_connect() 方法,它也只会创建一个连接

<?php

class database_connection  {
    private $hostname = 'localhost';
    private $username = 'root';
    private $password = '';
    private $database = 'assistant';
    private $database_handle = NULL;

    function connect() 
    {
        if ( ! isset($this->database_handle) ) {
            // only create a connection if one does not already exist
            $this->database_handle = mysqli_connect($this->hostname,
                                                    $this->username, 
                                                    $this->password, 
                                                    $this->database
                                                   );

            if (!$this->database_handle) {
                die('Could not connect to database!');
            } 
        }    

        return $this->database_handle;
    }

    function close() 
    {
        mysqli_close($this->database_handle);
        echo 'Connection closed!';
    }

}

?>

【讨论】:

  • 想知道为什么其他答案;给定you were asked by the OP to do so... 嗯........
  • 再次感谢 Riggs 的调查。我仍然掌握 PHP 中 OOP 的窍门,您的回答为我指明了正确的方向。
  • 也感谢您提供更多信息。作为解决方案,我将对此进行更深入的研究。
【解决方案2】:

你可以试试这个。

function close() 
{
    mysqli_close($this->database_handle);
    echo 'Connection closed!';
}

【讨论】:

    【解决方案3】:

    我认为您有一些范围界定问题,并且您没有在任何地方使用正确的参数。请尝试以下方法,如果有帮助,请告诉我。

    <?php
    
    class database_connection  {
        var $hostname = 'localhost';
        var $username = 'root';
        var $password = '';
        var $database = 'assistant';
        var $database_handle;
    
        function connect() 
        {
            $this->database_handle = mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
    
            if (!$this->database_handle) 
            {
                die('Could not connect to database!');
            } 
            else 
            {
                echo 'Connection established!';
                return $this->database_handle;
            }
        }
    
        function close($database_handle) 
        {
            mysqli_close($database_handle);
            echo 'Connection closed!';
        }
    }
    ?>
    
    
    <?php
    
    include 'includes/database_classes.php';
    
    $connection = new database_connection();
    $connection->connect();
    $query = 'SELECT * FROM  `users`';
    $result = mysqli_query($connection->database_handle, $query);
    
    if($numrows = mysqli_num_rows($result)) 
    {
        echo "<br>";
        echo $numrows;
        echo "<br>";
    
        while ($row = mysqli_fetch_assoc($result)) 
        {
            $database_username = $row['user_name'];
            $database_password = $row['user_password'];
            echo $database_username;
            echo $database_password;
        }
    }
    
    $connection->close($connection);   
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2015-05-27
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多