所以,我正在为我的一个客户编写Web应用程序,我决定将数据库连接保留在一个类中。我有以下代码:
class databaseConnection {
private $hostname = 'hn.app.dev';
private $username = 'root';
private $password = '';
private $database = 'hn_app_dev';
public function connect() {
$host = mysqli_connect($this->hostname, $this->username, $this->password);
if ($host) {
$connection = mysqli_select_db($host, $this->database);
if (!$connection) {
die('An error occured while trying to connect to the database.');
}
return $connection;
}
}
}
我正在使用mysqli_query
的标准PHP函数向数据库发送查询。我使用以下命令向数据库发送查询:
function fetch_from_db($query) {
$connection = new databaseConnection();
$query = mysqli_query($connection->$connection, 'QUERY');
}
我在代码中使用类是新手。我们仍在学习,就像我们所有人一样。我已经检查过但无法找到解决问题的方法。我知道问题是什么:这是一个类作为对象的问题,以及从中获取返回的$connection
变量的问题。
如何解决此问题,以便能够正确连接到我的数据库?此外,任何人都可以指出我可以学习修复的一些文档的方向,以便我将来可以解决这个问题。
谢谢!
答案 0 :(得分:2)
您可以通过许多不同的方式编写对象来处理与数据库的连接和查询。
最重要的是你想要实现的目标。
我认为这些是你想要的一些功能。
__construct()
这些是一些基本功能,可以轻松扩展。
class databaseConnection {
//private $hostname = 'hn.app.dev';
//private $username = 'root';
//private $password = '';
//private $database = 'hn_app_dev';
private $connection; // this is where the object will store the connection for other methods to access it
public function __construct($host, $username, $password, $database)
//public function connect() {
$this->connection = mysqli_connect($host, $username, $password);
if ($host) {
$this->connection->select_db($database);
if (!$this->connection) {
die('An error occured while trying to connect to the database.');
}
return $connection;
}
}
// this is so databaseConnection $db can access the connection for escaping MySQLi SQL
public function connection(){
return $this->connection;
}
function fetch_from_db($query) {
//$connection = new databaseConnection(); // I don't believe you really want to create a instance of this object inside of itself
//$query = mysqli_query($connection->$connection, 'QUERY');
$query = $this->connection->query($query); // we will use the object oriented style instead of the above procedural line
return $query; // return the results to the $results var in the bottom example
}
// this is a magic function that is called when the object is destroyed, so we will close the connection to avoid to many connections
function __destruct(){
$this->connection()->close();
}
}
// make a new datbaseConnection class with specific credentials
$db = new databaseConnection('localhost', 'someuser', 'somepass', 'somedb');
$sql = 'SELECT * FROM tableName LIMIT 100';
// call the fetch_from_db function from the new databaseConnection $db
$results = $db->fetch_from_db($sql);
// print the results
while($result = $results->fetch_assoc()){
print_r($result); // print_r on each row selected from db
}
您可以在手册中了解有关OOP和PHP对象的更多信息,并且在线提供了许多有关管理数据库连接和查询的特定类的教程。
希望这有帮助!
答案 1 :(得分:1)
如果您要将其保留在课程中,则永远不要调用connect()
函数。但是,如果您在启动课程时希望将其关联,请将connect()
功能更改为__construct()
并删除return
并将其分配给公共变量。
class databaseConnection {
private $hostname = 'hn.app.dev';
private $username = 'root';
private $password = '';
private $database = 'hn_app_dev';
public $connection;
public function __construct() {
$host = mysqli_connect($this->hostname, $this->username, $this->password);
if ($host) {
$connection = mysqli_select_db($host, $this->database);
if (!$connection) {
die('An error occured while trying to connect to the database.');
}
$this->connection = $host;
}
}
}
之后,您可以在函数中获取数据库连接,如:
function fetch_from_db($query) {
$connection = new databaseConnection();
$query = mysqli_query($connection->connection, 'QUERY');
}
现在,在说完所有这些之后,您不想在每个函数中创建一个新实例来访问数据库。因此可能将其设置为静态并创建一个init()
的各种函数,以便在整个应用程序中占用更少的内存。
对于课堂文档,PHP Classes/Objects页面将有所帮助。特别是'示例'链接。
答案 2 :(得分:0)
<?php
class databaseConnection {
private $hostname = 'localhost';
private $username = 'root';
private $password = '';
private $database = 'onlinylh_sggsfaculty';
public function connect() {
$host = mysqli_connect($this->hostname, $this->username, $this->password,$this->database);
if ($host) {
return $host;
}
else{
echo "Error";
}
}
}
function fetch_from_db($query) {
$conn = new databaseConnection();
$r = mysqli_query($conn->connect(), $query);
}
fetch_from_db()
?>
这对我有用。