Jump to content

passing mysqli object to an attribute of another class HELP!


Dizzizit

Recommended Posts

I don't know why it won't work.. as the topic titles says that I am trying to pass a mysqli object to a property in another class  but it keeps me getting an error.

here's the code for the mysqli object that i want to pass to another class

class ConnectMe2Db
{
	public $dbname  = 'somedatabase';
	public $dbuname = 'root';
	public $dbpass  = '';
	public $dbhost  = 'localhost';
	
	function __construct()
	{
		$mysqli = new mysqli($this->dbhost,$this->dbuname,$this->dbpass,$this->dbname)
			or die ('ERROR: '.$mysqli->connect_errno);
		return $mysqli;
	}
       # OTHER CODES...
}

and here is the class that i want the Mysqli object to pass to:

class DatabaseUsers
{
	private $dbconnection;
	
        function __construct()
	{
		$this->dbconnection = new ConnectMe2Db();#mysqli object will be passed to this attribute '$dbconnection'
	}
	
	public function session($username, $password)
	{
		$UserName = mysqli_real_escape_string($this->dbconnection,$username);
		$Password = mysqli_real_escape_string($this->dbconnection,md5($password));
		$querry = "SELECT * FROM trakingsystem.login WHERE username='$username' and password='$password'";
		$result = mysqli_query($this->dbconnection,$querry) or die (mysqli_error($this->dbconnection));
		$count = mysqli_num_rows($result);
		$row = mysqli_fetch_array($result);
		if ($count > 0)
			{
                              #some code here
			}
	}
#some other code here
}

and this outputs 4  errors:

#outputs 2 of these:

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli

and some

mysqli_query() expects parameter 1 to be mysqli

mysqli_error() expects parameter 1 to be mysqli

is there something wrong with the logic that I've made? please help thanks :D

Link to comment
Share on other sites

There's actually a whole lot of problems.

 

First of all, you cannot make the constructor of a class return an instance of an entirely different class. This doesn't even make sense. If you never intended to instantiate your ConnectMe2Db class, then why have it in the first place?

 

It seems you don't really know what you want. On the one hand, you've created that fancy wrapper class for MySQLi. On the other hand, you just use MySQLi directly without giving a damn about your class. On the one hand, you pretend to program object-oriented by instantiating the MySQLi class. But then you suddenly switch to the procedural interface and just use the MySQLi functions.

 

Before you write a single line of code, get clear about what you want. What is the goal you want to achieve? How do you want to achieve it? Don't just start typing in the hopes that something useful will come out of it.

 

Secondly, I have no idea why PHP programmers think it's a good idea to print all MySQL errors on the screen: or die (mysqli_error($this->dbconnection)). Do you realize that your users will see those messages on your website? What are they supposed to do with this? Help you debug your code? Actually, isn't it a rather bad idea to tell the whole world about your internal database structure?

 

The error messages of MySQL are meant for you, the programmer, not your users. Fixing errors is your job. So please remove all this terrible mysqli_error() stuff and start using proper error handling. PHP can actually do that all by itself. You want MySQLi to raise an error message whenever there's a problem? Turn on error reporting in the MySQLi driver:

$mysqli_driver = new mysqli_driver();
$mysqli_driver->report_mode = MYSQLI_REPORT_ERROR;

$database = new mysqli(...);

If you prefer exceptions over errors, append the MYSQLI_REPORT_STRICT flag.

 

The error system of PHP automatically sends the messages to the right device according to your configuration. During development, you'll want to see the errors right on the screen for easier debugging, so you turn display_errors on and log_errors off. In your live system, you want to log the errors and not show them on the screen, so you turn display_errors off and log_errors on. You may also want an special error page for your users, but that's another topic.

 

Last but not least, you need to learn how to use prepared statements with MySQLi. Manual escaping is obsolete (at least for the most part).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.