I keep having new questions so I'm starting this thread to neatly keep them all together. That's ok, I think? I hope I'm not asking too many questions
What I'm wondering right now is if I make a class--let's say User--and put it in a file--user.class.php--should any children of that class go in that file or should they have their own? Like Login could extend that class, Signup could, etc. Should they be in login.class.php and signup.class.php, respectively?
The better I get at programming, the more I appreciate arrays. Handy dandy things they are.
I normally keep them in separate files. This is particularly useful if you want to use an __autoload() function to take care of loading class definitions (which, however, requires a consistent naming convention so that your __autoload() function can figure out where a class's file is based on the class name).
PS: I'd probably have a User class (basically a data model class) and a separate UserAuth class (basically a controller-type class) with login() and register() methods which may accept and/or return a User object as applicable (i.e., using composition instead of inheritance in this case), since log-in and register are actions, not things, so you can't really say that "Login is a User" or "Register is a User."
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
user register, login, auth, changepass, etc.. should all be in the same class.
splitting them up has no benefit, having signup/login/register/auth etc.. classes all extending "user" just creates more overhead because in a proper system you'll have things from register, signup, login all cross referencing certain functions.
If you set it up properly ie;
$this->user->register();
$this->user->login();
$this->user->check_email();
or whatever.. you're putting everyting in the user class extending your main system core, this is what gives you the ability to add other things easily e.g.,
$this->log->insert('action','ip','date'); etc.
above your classes you can configure the autoload to do things automagically - or create an autoload function of only specifics, then each class in you're init() you'd put which extra classes you'd want to load ontop of it.
I wondering why this code is only finding one row in the accounts table, in the database, when there are two. When I var_dump the results it only has the data from the first row. What could be going on?
I'm assuming that for every row in the result set, this statement is somehow true, and then somehow when it gets to the end, it becomes false, but I do not know how.
Edit: And wow the var dump output looks a lot better when doing it your way
Last edited by evenstar7139; 07-29-2012 at 03:04 AM.
The better I get at programming, the more I appreciate arrays. Handy dandy things they are.
I'm assuming that for every row in the result set, this statement is somehow true, and then somehow when it gets to the end, it becomes false, but I do not know how.
Edit: And wow the var dump output looks a lot better when doing it your way
Basically think of it as foreach ($result AS $i=>$v) .. while mysql_fetch_assoc($doQuery) returns something - it does what you want it to.. where as with mysqli_fetch_assoc it does what you originally intended (which is fetch the entire result set into an assoc[] array.
Yeah, with the <pre> tags it definately makes it nicer/easier to read - it's very handy when debugging and working with API's and things of the sort.
Nope.. I'm assuming it just counts how many keys are in it before hand, then runs through it. where as while() adds 1 at a time until it returns null... im assuming :P
The variable $STH was created in the first line of code I showed. It was apparently created by having $DBH's method prepare() shoved into it. How did $STH get access to the method execute()? I don't see this as having been placed in $STH.
Thanks in advance.
The better I get at programming, the more I appreciate arrays. Handy dandy things they are.
The variable $STH was created in the first line of code I showed. It was apparently created by having $DBH's method prepare() shoved into it. How did $STH get access to the method execute()? I don't see this as having been placed in $STH.
Thanks in advance.
Just to sort of help you get used to interpreting the manual...
Assuming $DBH is a PDO object, you'll see from the manual that PDO::prepare() returns a PDOStatement object (or false on failure). One of PDOStatment's methods is execute(), so that is now accessible from the $STH object.
However, more robust code would have first checked that $STH was not false -- unless they have enabled PDO::ERRMODE_EXCEPTION earlier in the code?
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
However, more robust code would have first checked that $STH was not false -- unless they have enabled PDO::ERRMODE_EXCEPTION earlier in the code?
Yup, they had it on.
Originally Posted by NogDog
Just to sort of help you get used to interpreting the manual...
Assuming $DBH is a PDO object, you'll see from the manual that PDO::prepare() returns a PDOStatement object (or false on failure). One of PDOStatment's methods is execute(), so that is now accessible from the $STH object.
Yeah, I always have the manual open in a tab, as I refer to it frequently. Though, to be honest, sometimes I don't understand what it's saying. If I can't get answers from the manual, I see if Google can provide me with a plain-English explanation. Sometimes this works; sometimes it doesn't.
If all else fails, I post on a PHP forum (usually this one.) It's nice to have a source of guidance from real live people--gotten me out of so many frustrating situations over the years. I don't handle frustration the greatest. I probably would have given up were it not for people like you all. :)
PHP processed $DBH->prepare(), before it put it in $STH, so PDO::prepare() did not actually go in there, but rather its returned value did: PDO::execute(). Is this right?
I would try playing with this in my code, if I were not having a little problem. I just noticed that after removing mysql_query and mysql_select_db from my script, it's not connecting to MySQL anymore. Here is the relevant part of my code:
PHP Code:
private $dbHost = 'myhost'; //changed from what it really was private $dbUser = 'myusername'; //changed from what it really was private $dbPass = 'mypassword'; //changed from what it really was private $dbName = 'mydatabase'; //changed from what it really was private $DBH = false; public $result = array();
public function connect() { try { if (!$this->DBH) { $connectString="mysql:host={$this->dbHost};dbname={$this->dbName}, {$this->dbUser}, {$this->dbPass}"; $this->DBH = new PDO($connectString); $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); } } catch (PDOException $e) { print $connectString.'<br /><br />'.$e->getMessage(); }
}
I used the $connectString variable just so, upon receiving an error, I could output it to the screen and make sure that the values (like dbName) were actually getting used.
When I visit the page in my browser I get (sensitive details changed):
Okay, I changed the code, and the error I'm getting changed to this:
Notice: Undefined variable: DBH in /pathtomyfile/Database.class.php on line 22 Fatal error: Call to a member function setAttribute() on a non-object in /pathtomyfile/Database.class.php on line 22
PHP Code:
if (!$this->DBH) { $connectString="mysql:host={$this->dbHost};dbname={$this->dbName};"; $this->DBH = new PDO($connectString, $this->dbUser, $this->dbPass); $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); //this is line 22 }
The better I get at programming, the more I appreciate arrays. Handy dandy things they are.
Bookmarks