在其他类中使用MySQLi [英] Using MySQLi in other classes

查看:64
本文介绍了在其他类中使用MySQLi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助我解决我的小问题,但我无法解决.我要实现的是拥有一个database.php文件,该文件在每次调用它时都连接到数据库.然后,我希望能够获取结果,在其他文件中插入结果等,而不必在这些文件中单独连接数据库行.

I hope someone can help me with my small problem but I cannot figure it out. What I want to achieve is having one database.php file that connects to the database each time I call it. And then I want to be able to grab results, insert results etc in other files, but without having to have a seperate connect to the database line in those files.

我现在拥有的是我的数据库连接类:

What I have right now, is my database connection class:

<?php

$db_host    = 'localhost';
$db_user    = 'dbusername';        
$db_pass    = 'dbpassword';
$db_name    = 'dbname';

class database {

    public $mysqli;

    public function connect($db_host, $db_user, $db_pass, $db_name)
    {
        $this->mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);

        if ($this->mysqli->connect_errno)
        {
            return "An error occured while connecting to the database";
        }
    }
}
?>

现在,当我包含此文件并像这样建立连接时:

Now when I include this file and setup a connection like so:

$whatever = new database();

我希望能够使用其他类中的某些功能执行某些数据库操作,例如:

I want to be able to perform some database actions with some functions in other classes like so:

require_once("whatever.class.php");

$test = new myclass();

$update = $test->updataDatabase();

在我的"whatever.class.php"文件中,我具有以下功能:

And in my "whatever.class.php" file I have the following function:

public function grabResult($table, $where, $field)
{
    $result = 'SELECT * FROM '.$table.' WHERE '.$where.' = '.$field;
    $query = $this->mysqli->query($result);

无论我尝试什么,我总是会遇到以下错误:

And whatever I try, I always get the following error:

在第5行的/home/etc/public_html/whatever.class.php中的非对象上调用成员函数query()

Call to a member function query() on a non-object in /home/etc/public_html/whatever.class.php on line 5

基本上我的问题是,我希望能够在另一个类中使用"$ this-> mysqli"而不必在该类中再次设置数据库连接.我只想包含database.class.php文件,然后连接并能够使用$ this-> mysqli.

Basically what my question is, I want to be able to use "$this->mysqli" in another class without having to set up the database connection again in that class. I just want to include the database.class.php file and then connect plus being able to use $this->mysqli.

在另一页上,我发现了这一点:

From another page I found this:

$newConnection = new database;
$newConnection->connect($db_host, $db_user, $db_pass, $db_name);
$mysqli = $newConnection->mysqli;

但这并不能解决问题.与以下内容相同,无效:

But that didn't do the trick. Same as the following, did not work:

$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name);

public function __construct($mysqli)
{
    $this->mysqli = $mysqli;
}

我也尝试过从数据库扩展"myclass",但是没有运气,我尝试使用":: parent",也没有任何运气.自从我努力工作了几天以来,任何帮助都将不胜感激.

I have also tried extending the "myclass" from database, with no luck, I tried using "::parent", also without any luck. Any help is greatly appreciated since I been struggling with this for a few days now.

推荐答案

我认为您至少有3种方法可以做到这一点... (也许还有更多方法)

I think you have at least 3 ways to do this... (Maybe there are more ways)

1:第一个是将dbclass扩展到您的类,例如

1: The first one is to extend the dbclass to your class like

class myclass extends database {

    public function myclass(){

        parent::__construct();
        //....
    }
}

而不是在myclass

2 :第二种方法是

require_once(database.php);

class myclass {

    private $db;

    public function myclass(){  /* the constructor */

        $this->db = new database();
    }

    public function grabResult($table, $where, $field)
    {
        $result = "SELECT * FROM {$table} WHERE {$where}={$field}";

        return $db->mysqli->query($result);
    }
}

3:第三种方法是

class myclass{

    private $db;

    public function myclass(){
        $this->db = false;
    }

    public function setupDBHandler($dbHwNd){
        $this->db = $dbHwNd;
    }

    public function grabResult($table, $where, $field)
    {
        if($this->db){
            $result = "SELECT * FROM {$table} WHERE {$where}={$field}";
            return $db->mysqli->query($result);
        }
        else{
            return "Setup DBHandler first.";
        }
    }
}

这篇关于在其他类中使用MySQLi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆