Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

JD

JD Avatar



1,032


June 2008
Hey guys...

I'm a little confused as to why this isn't working. I've stripped it back to the bare minimum to make it simple to discuss here. So don't worry about SQL injection risks etc, and assume that all the connections and references are correct.

Other than that then, is this just an error with using queries inside functions inside classes or am I just being a total noob again?

class User  
{  

private $_display;  
     
public function __construct($username)  
{  

$mysqli = new mysqli("localhost", "username", "password", "database");
      $fetch_user = $mysqli->query("SELECT * FROM `registered_users` WHERE `user_name`='$username'");
$row = $fetch_user->fetch_assoc();

$this->_display = $row['user_display'];  
 
}  
     
}
$show1 = new User('username');  


If I call $show1->_display, it returns nothing. Testing the query etc outside of the function returns A-OK.

I remember coming across something like this before, but can't remember what I found.

Thanks!



Chris

Chris Avatar

******
Head Coder

19,519


June 2005
You set $_display to private, meaning it can't be accessed outside the class. :) also, if that isn't it, check for global issues before you stripped down the code (i.e. defined a variable outside of the scope.)

JD

JD Avatar



1,032


June 2008
I've set it to private public when debugging and the issue remained.

I also used the above code to test it away from any other errors, but the issue remained. I'm trying a different method now.


Last Edit: Jul 13, 2012 15:18:34 GMT by JD

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
This may sound stupid, but did you check mysqli_error() and make sure it didn't error? Did you make sure the row in $row exists? :P Without more source code, I'm not useful debugging.

Edit: And also stupid, but you -did- make sure that you changed the strings of "username" and "password" to their appropriate mix?


Last Edit: Jul 14, 2012 5:18:15 GMT by Chris

JD

JD Avatar



1,032


June 2008
Yeah - it doesn't error. The page itself returns

User Object
(
[_display:User:private] =>
)


And yeah, I just removed the username and password from here for security ;)

I'm honestly perplexed. Without more source code, this should still work -- it just... doesn't.



Last Edit: Jul 14, 2012 10:33:41 GMT by JD

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Can you post the CREATE TABLE command for the relevant MySQL table? :) Or at least the relevant parts. I want to recreate the setup as best as possible. :P

JD

JD Avatar



1,032


June 2008
I got it. Finally.

class MySQL {
private $_host;  
private $_username;
private $_password;
private $_database;
private $_conn;

public function __Construct($host, $username, $password){
// DO THE CONNECTING BUSINESS
}

public function load_database($set_database)
{  
// LOAD DATABASE
}

public function Fetch_User($username)
{
return $mysqli->query("SELECT * FROM `registered_users` WHERE `user_name`='$username'", $this->conn);
}

}



Cut the long bits out just to give you a general idea how I went about it...

Thanks for the help though!

newBookmarkLockedFalling