PHP数据库连接类问题 所以,我正在为我的一个客户编写一个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; } } }
我正在使用标准PHP函数PHP数据库连接类问题 所以,我正在为我的一个客户编写一个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; } } },php,mysql,class,Php,Mysql,Class,我正在使用标准PHP函数mysqli\u query向数据库发送查询。我使用以下命令向数据库发送查询: function fetch_from_db($query) { $connection = new databaseConnection(); $query = mysqli_query($connection->$connection, 'QUERY'); } 我不熟悉在代码中使用类。我仍在学习,就像我们大家一样。我已经检查了,但找不到解决问题的方法。我知道问题是什
mysqli\u query
向数据库发送查询。我使用以下命令向数据库发送查询:
function fetch_from_db($query) {
$connection = new databaseConnection();
$query = mysqli_query($connection->$connection, 'QUERY');
}
我不熟悉在代码中使用类。我仍在学习,就像我们大家一样。我已经检查了,但找不到解决问题的方法。我知道问题是什么:这是类作为对象的问题,与从中获取返回的$connection
变量有关
如何解决此问题,以便正确连接到数据库?另外,有谁能告诉我一些文档的方向,我可以学习修复方法,以便我将来能够解决这个问题
谢谢大家! 如果要将其保存在类中,则永远不要调用
connect()
函数。但是,如果您希望在初始化类时将其连接起来,请将connect()
函数更改为\uu 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的页面将有所帮助。特别是“示例”链接。如果要将其保留在类中,则永远不要调用
connect()
函数。但是,如果您希望在初始化类时将其连接起来,请将connect()
函数更改为\uu 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;
}
}
}
<?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()
?>
之后,您可以在函数中获得数据库连接,如:
function fetch_from_db($query) {
$connection = new databaseConnection();
$query = mysqli_query($connection->connection, 'QUERY');
}
现在,在说了所有这些之后,您不想在每个函数中创建一个新实例来访问数据库。因此,可能会将其设置为静态,并创建一个init()
函数,使其在整个应用程序中占用更少的内存
对于类文档,PHP的页面将有所帮助。特别是“示例”链接。
<?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()
?>
这对我很有用。
这对我很有用。有很多不同的方法可以编写对象来处理与数据库的连接和查询 最重要的是你想要实现什么 我想这是你想要的一些功能
- 单连接
- 运行SQL并返回mysqli结果
- 访问用于转义值的连接
- 看起来您想将凭据存储在它自己的对象中。(我建议将它们作为值传入
\uu 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对象的更多信息,并且有许多在线教程专门介绍用于管理数据库连接和查询的类
希望这有帮助!有许多不同的方法可以编写对象来处理与数据库的连接和查询 最重要的是你想要实现什么 我想这是你想要的一些功能
- 单连接
- 运行SQL并返回mysqli结果
- 访问用于转义值的连接
- 看起来您希望将凭据存储在对象本身中。(我建议将它们作为值传入
\uu 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对象的更多信息,并且有许多在线教程专门介绍用于管理数据库连接和查询的类
希望这有帮助!$connection->connect()@Federico他使用的是
mysqli
函数,而不是mysql
my bad,我读错了$connection->connect()
@Federico他使用的是mysqli
函数,而不是mysql
我的错,我读错了,我得到的是一个布尔值,它是“1”,而不是连接本身。知道为什么吗?谢谢。哦,布尔值,因为mysql\u select\u db
返回true或false,但在原始连接上仍然有效。更新了代码检查示例。我得到的是一个布尔值响应,它是“1”,而不是连接本身。知道为什么吗?谢谢。哦,布尔值,因为mysql\u select\u db
返回true或false,但在原始连接上仍然有效。更新的代码示例。非常好且经过深思熟虑的答案。+1非常好且经过深思熟虑的答案。+1