Jump to content

scope of mysqli


ToonMariner

Recommended Posts

Hi,

Having a spot of bother using the mysqli OO stuff.

OK here's the connection...
[code]<?php
@ $mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}
?>[/code]

I have written a login class and within it is the following constructor...

[code]<?php
class login_check {
function __construct() { // initialize class properties.
// ensure no mysql injection in usernamer or password        
  if (isset($_POST['username'], $_POST['password'])) {
   $this->username = $mysqli->real_escape_string($_POST['username']);
   $this->password = $mysqli->real_escape_string($_POST['password']);
  }
}

....

}
?>[/code]

I get the follwing error...

Notice: Undefined variable: mysqli in C:\Program Files\Apache Group\Apache2\htdocs\f_m_cms\classes\login.php on line 16

Fatal error: Call to a member function real_escape_string() on a non-object in C:\Program Files\Apache Group\Apache2\htdocs\f_m_cms\classes\login.php on line 16

and line 16 is of course....

$this->username = $mysqli->real_escape_string($_POST['username']);

I am assuming this is a scope thing...If so how do I ensure that objects are passed to other classes when they are needed?
Link to comment
https://forums.phpfreaks.com/topic/4937-scope-of-mysqli/
Share on other sites

It is a scoping thing. You need to figure out a way to have access to it all through your program, a way that seems elegant to you. You can use global $mysqli at the top of every function, that's pretty easy.

What I did was build a class around mysqli that handles everything, and it stashes the link in a global variable, so that when you instantiate again, it can just check for the global variable and skip connecting.
Link to comment
https://forums.phpfreaks.com/topic/4937-scope-of-mysqli/#findComment-17731
Share on other sites

Silly man.. you didn't define what $mysqli was..

it has to be inside of each function.

[code] if (isset($_POST['username'], $_POST['password'])) {[/code]

won't work either.

try this..

[code]class login_check {

    private $USER_LOGIN;
    private $USER_PASSWORD;
    
    public function __construct($login, $pass){
        
        $mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
        
        $this->USER_LOGIN    = $mysqli->real_escape_string($login);
        $this->USER_PASSWORD = $mysqli->real_escape_string($pass );
        
        $this->checkLogin(); // check to make sure login exists;
    }
    
    public function checkLogin() {
        $mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
        $results = $mysqli->query("select * from users where login = $this->USER_LOGIN AND pass = $this->USER_PASSWORD");
        
        // do some checking here real quick (loop through returned data, count the number of returned rows, whatever..)
    }
    
}[/code]

Notice the difference? If you need any help or have any questions with this.. don't hesitate!
Link to comment
https://forums.phpfreaks.com/topic/4937-scope-of-mysqli/#findComment-17734
Share on other sites

Thanks...

two questions....

If the $_POST vars are super globals then surely they will be available within the class???? (I can echo out those values from the class)

I have already created an instance of the database connection object outside the class so it would be nice to use that object inside another class... why do I need to create another database connection object when one is already available??

Basically I aint got a good handle on the scope of objects and variables witihn classes yet (still reading) but any guidance is much appreciated.
Link to comment
https://forums.phpfreaks.com/topic/4937-scope-of-mysqli/#findComment-17749
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.