dpd20130 2014-07-28 08:00
浏览 43
已采纳

在其他类中使用MySQLi

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();

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:

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

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;
}

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.

  • 写回答

1条回答 默认 最新

  • duannan3959 2014-07-28 08:06
    关注

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

    databasde.php (with class) needs to be included first for following steps

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

    class myclass extends database {
    
        public function myclass(){
    
            parent::__construct();
            //....
        }
    }
    

    than you have all functions and vars in your myclass

    2: The second way is

    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: The third way is

    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.";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题