ToonMariner Posted March 14, 2006 Share Posted March 14, 2006 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]<?phpclass 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 16Fatal 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 16and 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? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 14, 2006 Author Share Posted March 14, 2006 *bump*Desperate for some insight on this guys n gals.... Quote Link to comment Share on other sites More sharing options...
wickning1 Posted March 15, 2006 Share Posted March 15, 2006 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. Quote Link to comment Share on other sites More sharing options...
keeB Posted March 15, 2006 Share Posted March 15, 2006 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! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 15, 2006 Author Share Posted March 15, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.