Jump to content


Photo

scope of mysqli


  • Please log in to reply
4 replies to this topic

#1 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 14 March 2006 - 03:55 PM

Hi,

Having a spot of bother using the mysqli OO stuff.

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

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

<?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']);
  }
 }

....

}
?>

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?
follow me on twitter @PHPsycho

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 14 March 2006 - 08:04 PM

*bump*

Desperate for some insight on this guys n gals....
follow me on twitter @PHPsycho

#3 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 15 March 2006 - 08:06 AM

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.

#4 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 15 March 2006 - 08:24 AM

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

it has to be inside of each function.

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


won't work either.

try this..

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..)
    }
    
}

Notice the difference? If you need any help or have any questions with this.. don't hesitate!

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#5 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 15 March 2006 - 09:34 AM

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.
follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users