我目前正在学习 PHP,并试图弄清楚如何使用 OOP 连接名为 (lead_gen_business) 的 MySQL 数据库。顺便给我一个任务来获取具体的输出。我的问题是我很难连接到我的数据库。对于代码的上下文。我创建了一个数据库,里面的代码是我针对 OOP 修改的预构建函数。
数据库
class Database
{
public $connection;
public function __construct()
{
if (!defined('DB_HOST')) {
define('DB_HOST', 'localhost');
}
if (!defined('DB_USER')) {
define('DB_USER', 'root');
}
if (!defined('DB_PASS')) {
define('DB_PASS', 'Yourpass');
}
}
public function connect($db_name)
{
$this->connection = new mysqli(DB_HOST, DB_USER, DB_PASS, $db_name);
}
/*-----------------------END OF CONNECTION PROCESS------------------------*/
/*----------------------DATABASE QUERYING FUNCTIONS-----------------------*/
//SELECT - used when expecting single OR multiple results
//returns an array that contains one or more associative arrays
function fetch_all($query)
{
$data = array();
global $connection;
$result = $connection->query($query);
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
return $data;
}
//SELECT - used when expecting a single result
//returns an associative array
function fetch_record($query)
{
global $connection;
$result = $connection->query($query);
return mysqli_fetch_assoc($result);
}
//used to run INSERT/DELETE/UPDATE, queries that don't return a value
//returns a value, the id of the most recently inserted record in your database
function run_mysql_query($query)
{
global $connection;
$result = $connection->query($query);
return $connection->insert_id;
}
//returns an escaped string. EG, the string "That's crazy!" will be returned as "That\'s crazy!"
//also helps secure your database against SQL injection
function escape_this_string($string)
{
global $connection;
return $connection->real_escape_string($string);
}
}
require_once('database.php');
class Sites extends Database
{
public $client;
public $havingArr = [];
public $count = "COUNT(*)";
public $id;
public function select($myClient)
{
// Assuming $myClient is an array containing column names
foreach ($myClient as $data) {
$this->client = $data;
}
return $this;
}
public function group_by($siteId)
{
$this->id = $siteId;
return $this;
}
public function having($paraCount, $greaterThan, $number)
{
// Building having condition
$this->havingArr[] = "$paraCount $greaterThan $number";
return $this;
}
public function get()
{
// $havingCondition = implode(" AND ", $this->havingArr);
// $query = "SELECT {$this->client}, {$this->count} FROM sites
// GROUP BY {$this->id} HAVING $havingCondition";
$query = "SELECT * FROM sites";
// Assuming $this->connection->fetch_all is a method that executes the query and fetches all rows
$result = $this->fetch_all($query);
return $result;
}
}
最后...
include('database.php');
include('sites.php');
class Query_builder extends Sites
{
public function __construct($db_name)
{
$this->connect($db_name);
}
public function show_get()
{
$datas = $this->get();
foreach ($datas as $data) {
echo $data['site_id'] . "<br>";
echo $data['domain_name'] . "<br>";
}
return $this;
}
}
$show = new Query_builder('lead_gen_business');
var_dump($show->show_get());
在此之前我遇到了很多错误,最后一个错误是这个。
致命错误:未捕获错误:在第 38 行的 C:\wamp64\www\OOP Fundamentals\Advance OOP\Query Builder\database.php 中对 null 调用成员函数 query()
抱歉,如果太长了。我已经花了一天时间试图独立解决这个问题。任何答案都非常感谢!预先感谢。
您在其中一个函数中使用了
global $connection
,但是当您创建连接时,您将其分配给了 $this->connection
,一个类变量。
我认为您忽略的重要事实是这些是不同的变量。
您将连接分配给
$this->connection
,但这不会将连接分配给全局变量 $connection
。类变量不是全局变量。
然后,当您尝试在其中一个函数中使用
global $connection
时,该变量没有分配任何内容。在PHP中,你可以引用没有初始化的变量,但最初只是null
。
这就是为什么当您尝试致电
$connection->query()
时收到错误:
未捕获错误:在 null 上调用成员函数 query()
具有值
null
的变量不是对象,并且没有成员函数。这就像尝试对 true
值或整数 42
调用成员函数。
$my_var = 42;
$my_var->query(); // this would cause the same error
如果将连接分配给
$this->connection
,则必须在该类的后续函数中使用相同的引用。
例如:
function fetch_all($query)
{
$data = array();
$result = $this->connection->query($query);
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
return $data;
}
这里函数使用
$this->connection
,假设它是通过先前调用类的 connect()
函数设置的。
还有机会以其他方式改进你的课程,所以什么是 OOP 方式的答案可能会更长。但我只回答您在上面的帖子中显示的错误的原因。