我觉得我在PHP中缺少一些关于OOP的东西。我已经阅读了许多关于连接到mysql的教程但是仍然遇到错误,告诉我我的数据库句柄不存在,即使它确实连接了。我有以下代码,一切都正确连接,但是当我尝试关闭连接时(我知道PHP在脚本完成时关闭了连接,但这对我来说也是一个学习练习)与$ connection->关();我注意到$ database_handle未定义,因此mysqli_close($ database_handle)引用了一个NULL变量并返回致命错误。
为什么$ connection-> connect(); work和$ 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();
?>
答案 0 :(得分:2)
我是否可以建议您采用这种方法,以便每个人在脚本上一次只有一个连接
这也解决了你的错误
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!';
}
}
?>
答案 1 :(得分:0)
你可以试试这个。
function close()
{
mysqli_close($this->database_handle);
echo 'Connection closed!';
}
答案 2 :(得分:-1)
我认为你有一些范围问题,而且你并没有在任何地方使用正确的参数。请尝试下面的内容,如果有帮助请告诉我。
<?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);
?>