Hi all

I'm trying to set my mysqli connection to my database as outlined in http://php.net/manual/en/mysqli.quickstart.connections.php , with one difference - I'm trying to put my connection parameters in an include file outside of my root folder, to try to secure my DB password.

$mysqli = new mysqli("DBServer", "DBUsername", "DBPassword", "DBName");  //set connection params
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    exit();

Now, with my query object, I can confirm that the connection is established, and the connection file is included when I launch my class_lib file, but, when I try to use the connection object, it returns "Call to a member function query() on a non-object in filename on line 21"

The code for my object is as follows:

class siteConfig {
    protected $results = array();
    public function set_siteConfig() {
        $res = $mysqli->query($mysqli,"SELECT status FROM `site-config`");
        $this->results = $res->fetch_assoc();
        $mysqli->close();
    }
    public function get_siteConfig() {
        return $this->results;
        }
}

The error message is telling me that the error is in the line "$res = $mysqli->query($mysqli,"SELECT * FROM site-config");"

How can I correctly implement my database connection, while, at the same time, keeping my database credentials secure?

Thanks for any suggestions you guys might have!

-Jay

Member Avatar for diafol

The mysqli object is not global (good thing), so you need to pass it as a parameter to your class.

$db = new mysqli(....);

class siteConfig {
    protected $results = array();
    private $db;

    public function __construct($db)
    {
        $this->db = $db; 
    }
    public function set_siteConfig() {
        $res = $this->db->query($mysqli,"SELECT status FROM `site-config`");
        $this->results = $res->fetch_assoc();
        //don't close it here
    }
    public function get_siteConfig() {
        return $this->results;
    }
}

If you didn't want to use it in the constructor or you do not want to use and constructor at all, then pass it to set_siteConfig(). If the db object is not being used anywhere else in the class, then you doon't even have to roll it into a property.

Thank you for the assistance, diafol!

I still have to read up more on constructors, though (no matter what I've read on them, I still can't seem to grasp the advantage of using them), so I'll just set the value in the method...

Have a great weekend!

Looks like I may have spoken too soon....

I have tried subbing in your suggestion via copy/paste, and inserting my DB connection credentials, but I'm getting a new error on top of the original one:

Warning: Missing argument 1 for siteConfig::__construct(), called in /home/website/website.com/index.php on line 3 and defined in /home/website/website.com/includes/class_lib.php on line 25

Line 3 of index.php just instantiates the siteConfig object ($siteconfig = new siteconfig();), and line 25 of class_lib.php is

public function __construct($db)

I'm also still getting the error

Fatal error: Call to a member function query() on a non-object in /home/roadtostage/roadtostage.com/includes/class_lib.php on line 30

with line 30 being

$res = $this->db->query($mysqli,"SELECT status FROM `site-config`");

Looking at that line more closely, shouldn't $mysqli be $db? Either way, changing that variable didn't make the error go away. I'm just wondering if that would need to be corrected/changed after I addressd the fatal error.

Either way, thanks!

Member Avatar for diafol

Sorry the line:

$this->db->query($mysqli,"SELECT status FROM `site-config`");

SHould just be:

$this->db->query("SELECT status FROM `site-config`");

I copy and pasted from your original code without checking it. If you're using OOP verion of mysqli, then you shouldn't provide the connection object.

Any class method that has a "non-optional" parameter, must be supplied:

$db = new mysqli(....);
$siteconfig = new siteconfig($db);

Otherwise you'll get the error noted. Sorry perhaps I should have stated this.

The whole point of a constructor (IMO) is to 'set up' the class ready to use. You draw in all the vital components.

This can sometimes be left to other 'normal methods'. Indeed some constructors
don't have any parameters. Some classes don't even have constructors. If there is nothing to do when a class is initialized or they do not requir any external input to function, then often they are not required.

Other contributors better versed in OOP than me may be able to offer you a better explanation.

That took care of it!

I'm getting a better grasp of how the mysqli object works, now. It'll take some time to 'master' it, but you've pointed me in the right direction, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.