[英]PHP code convert to PHP/MySQLi OOP
today i tried to convert my code to PHP/MySQLi OOP code. 今天,我试图将我的代码转换为PHP / MySQLi OOP代码。
class Database
{
private $host;
private $user;
private $password;
private $db;
private $mysqli;
function __construct()
{
$this->host = "*****";
$this->user = "*****";
$this->password = "******";
$this->db = "*****";
$this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->db);
if (mysqli_connect_errno()):
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
endif;
}
}
This is a script for the query's: 这是查询的脚本:
include_once("WD_Config/database.php");
class Adressen_Db
{
function __construct()
{
$this->database = new Database();
}
public function selecteer()
{
$query = "SELECT * FROM wd_adressen WHERE verborgen = 0 ORDER BY naam ASC";
$result = $this->database->mysqli->query($query);
return $result;
}
}
And this is how i call it. 这就是我所说的。
$adressen = new Adressen_Db;
$adressen_result = $adressen->selecteer();
echo "<p>";
while ($row = $adressen_result->fetch_assoc()):
echo "<a href='http://maps.google.com/?q=".$row['voladres']."' target='_blank'>".$row['naam']."</a> woonachtig op <i>".$row['voladres']."</i><br>";
endwhile;
echo "</p>";
I alway get a "Call to a member function query() on a non-object". 我总是得到“在非对象上调用成员函数query()”。 Doesn't matter what i trie ...
没关系,我试着...
Can somebody tell me why that is? 有人可以告诉我为什么吗?
Thanks! 谢谢!
The $mysqli
variable in class Database is declared private
. 类Database中的
$mysqli
变量声明为private
。
You can access it only through setters and getters. 您只能通过设置器和获取器访问它。
I think while you definitely need to have $mysqli
as public so it can be accessed in the other method, there might be something else, as the error would be something like 我认为虽然您绝对需要将
$mysqli
作为公共目录,以便可以用另一种方法访问它,但可能还有其他原因,因为错误可能是这样的
trying to access private property in database class
试图访问数据库类中的私有属性
or something like that, whereas your script throws a non-object call error 或类似的东西,而您的脚本会引发非对象调用错误
I think your new Adressen_Db;
我认为您是
new Adressen_Db;
lacks the parenthesis: 缺少括号:
$adressen = new Adressen_Db();
You can replace your code with this: 您可以使用以下代码替换代码:
Config.php config.php文件
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "your_database_name");
Now include this file in your database file 现在将此文件包含在数据库文件中
require_once 'config.php';
Class Database {
public $host = DB_HOST;
public $user = DB_USER;
public $pass = DB_PASS;
public $dbname = DB_NAME;
public $link;
public $error;
public function __construct() {
$this->getConnection();
}
private function getConnection() {
$this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
if (!$this->link) {
$this->error = "Connection failed" . $this->link->connect_error;
return false;
}
}
// for only select query
public function select($query) {
$result = $this->link->query($query) or
die($this->link->error . __LINE__);
if ($result->num_rows > 0) {
return $result;
} else {
return false;
}
}
// for insert, delete and update
public function myquery($query) {
$myquery = $this->link->query($query) or
die($this->link->error . __LINE__);
if ($myquery) {
return $myquery;
} else {
return false;
}
}
}
Now, make your queries like this: 现在,使您的查询像这样:
<?php
require_once './lib/Database.php';
?>
<?php
class Admin {
private $db;
public function __construct() {
$this->db = new Database();
}
public function getData(){
$query = "SELECT * FROM admin";
$result = $this->db->select($query);
if($result != false){
while($row = $result->fetch_assoc()){
// do your thing
}
}
}
public function insert(){
$query = "INSERT INTO admin(admin_name) VALUES('$admin_name')";
$result = $this->db->myquery($query);
if($result){
$msg = "User has been added successfully.";
return $msg;
} else {
$msg = "Error while adding user. Please try again.";
return $msg;
}
}
}
Do this. 做这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.