coldfiretech Posted September 6, 2008 Share Posted September 6, 2008 Hello all, i started php programming about 2 weeks ago and have been directed to dive head first in to OOP, i have read the OOP tutorials here on the site and am trying to make my first class file.. I was working thru the tutorial and i cant figure out where to pass the login credentials to the mysqli function.. Like i said im a noob so.... could someone please show me how to do this.. class UserDataMapper { private $_db; public function __construct(mysqli $db) { $this->_db = $db; //WHERES THE CREDENTIALS GO?? } public function find($username) { $result = $this->_db->query("SELECT * FROM user WHERE username = $username"); $row = $result->fetch_assoc(); return new User($row['username'], $row['password'], $row['logged_in']); } Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 6, 2008 Share Posted September 6, 2008 It looks like you would do it like this: $mysqli = new mysqli($host,$user,$password); $userDataMapper = new UserDataMapper($mysqli); So you instantiate mysqli object outside your class, and then pass it to constructor when creating an instance of UserDataMapper. Quote Link to comment Share on other sites More sharing options...
coldfiretech Posted September 6, 2008 Author Share Posted September 6, 2008 Thanks, im confused though ???... Maybe i need to read more. Quote Link to comment Share on other sites More sharing options...
coldfiretech Posted September 6, 2008 Author Share Posted September 6, 2008 Okay i read that tutorial again and here is what i come up with and its not working ???. I am getting this error. Fatal error: Call to a member function query() on a non-object in C:\wamp\www\csx\testclass.php on line 19 TestClass.php <?php error_reporting(E_ALL); define("csx_host", "localhost"); define("csx_user", "matt"); define("csx_pass", "****"); define("csx_db", "testdb"); $mysqli = new mysqli(csx_host, csx_user, csx_pass, csx_db); class csx_functions { private $_db; function _construct($mysqli) { $this->_db = $db; } public function test() { $result = $this->_db->query("SELECT login FROM USERS"); $row = $result->FETCH_ASSOC(); return $row; } } ?> Index.php <html> <title>testing class files</title> <?php include("testclass.php");?> <body> <?php error_reporting(E_ALL); $csx = new csx_functions(); $test = $csx->test(); echo $test; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 6, 2008 Share Posted September 6, 2008 Yout index.php should look like this <html> <title>testing class files</title> <?php include("testclass.php");?> <body> <?php error_reporting(E_ALL); $csx = new csx_functions($mysqli); //here you're passing $mysqli object to $csx object, so that it could use database connection. $test = $csx->test(); echo $test; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
coldfiretech Posted September 6, 2008 Author Share Posted September 6, 2008 I tried that and its still not working. I am trying to make a class file with all my functions in it that connect to the database so i can do some validation and inserts and updates... things of that nature. I havent done any oo programming before and i feel over my head right now. For instance i would like to make a class to use to register a user into the database. But i dont really understand how i would set up the registration page itself to talk to that class and to tell it what action to do. and then how to actually do the registration. I mean i know how to connect to db and do inserts and what not but not like in oo... its like a whole new language! I could really use some help, maybe a small example of how to do the registration thing, nothing fancy just a simple whatnot to get me understanding. Im just stuck right now, if i have to read that tutorial again the lump on my forehead is going to get larger! haha, thanks! Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 6, 2008 Share Posted September 6, 2008 In your testclass.php function _construct($mysqli) { $this->_db = $db; } should be function _construct($mysqli) { $this->_db = $mysqli; } Quote Link to comment Share on other sites More sharing options...
coldfiretech Posted September 6, 2008 Author Share Posted September 6, 2008 should be function _construct($mysqli) { $this->_db = $mysqli; } i changed it to this and it still gives me the same error Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 6, 2008 Share Posted September 6, 2008 Could you add: echo $mysqli->server_info; after $mysqli = new mysqli(csx_host, csx_user, csx_pass, csx_db); to check if there's a connection established? Quote Link to comment Share on other sites More sharing options...
coldfiretech Posted September 6, 2008 Author Share Posted September 6, 2008 This is what i get 5.0.51b-community-nt Fatal error: Call to a member function query() on a non-object in C:\wamp\www\matt\testclass.php on line 21 Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 6, 2008 Share Posted September 6, 2008 Ok. So we've a working connection, but it doesn't get passed to your object. Oh! I see it The function should be called __construct not _construct (two underscores, not one) It's a special function that is called automatically, when you create a new object Quote Link to comment Share on other sites More sharing options...
coldfiretech Posted September 6, 2008 Author Share Posted September 6, 2008 Thanks, i will try this and report back. Quote Link to comment Share on other sites More sharing options...
coldfiretech Posted September 6, 2008 Author Share Posted September 6, 2008 Okay so i got rid of that error now i just cant get it to properly return data from the db. I really feel like stuffing my head in the sand here! OO is some seriously hard stuff to get down.. I guess i could be a little over anxious. I just started OOP 2 days ago. testclass.php <?php error_reporting(E_ALL); define("csx_host", "localhost"); define("csx_user", "stacks"); define("csx_pass", "island67"); define("csx_db", "cellsavior"); $mysqli = new mysqli(csx_host, csx_user, csx_pass, csx_db); class csx_functions { protected $_db; function __construct($mysqli) { $this->_db = $mysqli; } public function test($query) { $result = $this->_db->query($query); return $result->FETCH_ASSOC(); echo $result; } } ?> index.php <html> <title>testing class files</title> <?php include("testclass.php");?> <body> <?php error_reporting(E_ALL); $csx = new csx_functions($mysqli); //here you're passing $mysqli object to $csx object, so that it could use database connection. $test = $csx->test("SELECT * FROM USERS"); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 6, 2008 Share Posted September 6, 2008 Your 'test' method uses mysqli extension in a wrong way. See manual for information and examples. You could use something lie this for example. Try to figure out, why this way, and what are the functions used. public function test($query) { $result = $this->_db->query($query); while ($row = $result->fetch_assoc()) { print_r($row); } } Quote Link to comment Share on other sites More sharing options...
coldfiretech Posted September 7, 2008 Author Share Posted September 7, 2008 Thanks, i will try this... OOP is tough but im refusing to let it beat me! Quote Link to comment Share on other sites More sharing options...
coldfiretech Posted September 8, 2008 Author Share Posted September 8, 2008 Okay so here is what i have come up with... Cant get it to work... Dont know what im doing wrong... Its giving me this error Notice: Undefined property: csx_functions::$db in C:\wamp\www\matt\testclass.php on line 20 Fatal error: Call to a member function query() on a non-object in C:\wamp\www\matt\testclass.php on line 20 index.php <?php error_reporting(E_ALL); $csx = new csx_functions($mysqli); //here you're passing $mysqli object to $csx object, so that it could use database connection. $result = $csx->check_email("coldfiretech@yahoo.com"); if ($result > 0) { //username is good to use echo "no good"; }else{ //username is no good echo "good"; } ?> testclass.php <?php error_reporting(E_ALL); define("csx_host", "localhost"); define("csx_user", "matt); define("csx_pass", "*****); define("csx_db", "mydb"); $mysqli = new mysqli(csx_host, csx_user, csx_pass, csx_db); class csx_functions { protected $_db; function __construct($mysqli) { $this->_db = $mysqli; } public function check_email($email) { $query = "SELECT `email` from users WHERE email = '$email'"; $r = $this->db->query($query); if(mysqli_num_rows($r) >0){ RETURN 0; }else{ RETURN 1; } } } ?> Quote Link to comment Share on other sites More sharing options...
coldfiretech Posted September 8, 2008 Author Share Posted September 8, 2008 Nevermind... Ive got it. Figured it out on my own. 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.