www.webdeveloper.com
Page 1 of 14 12311 ... LastLast
Results 1 to 15 of 198

Thread: My questions thread.

  1. #1
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357

    My questions thread.

    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.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,912
    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

    eBookworm.us

  3. #3
    Join Date
    Jul 2012
    Location
    Vancouver
    Posts
    61
    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.

  4. #4
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    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?

    PHP Code:
    class Database
    {
        private 
    $dbHost 'localhost';     
        private 
    $dbUser 'myusername';         
        private 
    $dbPass 'mypassword';        
        private 
    $dbName 'mydatabase'
        private 
    $dbConn false;
        public 
    $result = array();    
         
         public function 
    connect()
         {
             if (!
    $this->dbConn$this->dbConn=mysql_connect($this->dbHost,$this->dbUser,$this->dbPass) or die(mysql_error());
         }
         
         public function 
    setDB()
         {                  
             if (
    $this->dbConnmysql_select_db($this->dbName) or die(mysql_error());
         }
         
         public function 
    select($query)
         {
             
    $doQuery=@mysql_query($query) or die(mysql_error());
             if (
    $doQuery)
                {
                      
    $this->result mysql_fetch_assoc($doQuery);
                 }
         }
         
         public function 
    getResult()
         {
             return 
    $this->result;
         }
         
         public function 
    displayResults()
         {
             foreach (
    $this->result as $key => $value)
             {
                  print 
    $key.': '.$value.'<br />';
             }
         }
         
         public function 
    flushResult()
         {
             
    $this->result=array(); 
         }
    }

    $accounts=new Database();
    $accounts->connect();
    $accounts->setDB();
    $accounts->select("select * from accounts");
    //$accounts->displayResults();
    var_dump($accounts->getResult()); 
    var_dump outputs this (some identifying details changed):
    array(11) { ["userID"]=> string(1) "1" ["username"]=> string(8) "sparkles" ["loginName"]=> string(16) "skittlesskittles" ["staffRanking"]=> string(1) "1" ["email"]=> string(20) "myemail@awebsite.com" ["password"]=> string(10) "mypassword" ["birthdate"]=> string(10) "0000-00-00" ["gender"]=> string(6) "female" ["activated"]=> string(3) "yes" ["activationCode"]=> string(10) "12345ABCDE" ["playedBefore"]=> string(3) "Yes" }
    I probably did 100000000000 things wrong. Feel free to pick this code apart :P
    Last edited by evenstar7139; 07-29-2012 at 12:51 AM.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  5. #5
    Join Date
    Jul 2012
    Location
    Vancouver
    Posts
    61
    Well aside from the obvious - you shouldnt be using mysql extension.. at a minimum use mysqli (mysql improved) or PDO.

    Make the adjustment here and it will work
    PHP Code:
             if ($doQuery)
                {    
                     while(
    $res mysql_fetch_assoc($doQuery)) {
                        
    $this->result[] = mysql_fetch_assoc($doQuery);  
                     }
                     
                 } 
    On a side note.. if you want to print arrays nicely..

    PHP Code:
    echo "<pre>";
    print_r($accounts->getResult()); 
    echo 
    "</pre>"

  6. #6
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    How does this part of the code work:

    PHP Code:
    while($res mysql_fetch_assoc($doQuery)) 
    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.

  7. #7
    Join Date
    Jul 2012
    Location
    Vancouver
    Posts
    61
    Quote Originally Posted by evenstar7139 View Post
    How does this part of the code work:

    PHP Code:
    while($res mysql_fetch_assoc($doQuery)) 
    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.

  8. #8
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    I always wondered how something like foreach() knows to move the row pointer down each time it loops. Do you know?
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  9. #9
    Join Date
    Jul 2012
    Location
    Vancouver
    Posts
    61
    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

  10. #10
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    What happened right here. I just saw this in a PDO tutorial and I'm not sure what's going on with the $STH variable:

    PHP Code:
    $STH $DBH->prepare("INSERT INTO folks ( first_name ) values ( 'Cathy' )");  
    $STH->execute(); 
    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.

  11. #11
    Join Date
    Jul 2012
    Location
    Vancouver
    Posts
    61
    It's to keep it OOP..

    With a db class you can continously add more/remove variables and grab data and not have to rewrite things.

    So if you wanted to do 2 queries in the same page with only 1 minor difference..

    PHP Code:
    $dbrun $dbh->prepare('SELECT *
        FROM tables
        WHERE some_type < ? AND level = ?'
    );

    $dbrun->execute(array(2'admin'));
    $result_set_one $dbrun->fetchAll();
    $dbrun->execute(array(1'user'));
    $result_set_two $dbrun->fetchAll(); 
    Its very simple here, but as you start using it more you'll see the benefits ;D
    Last edited by mistin.ca; 07-30-2012 at 09:11 AM.

  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,912
    Quote Originally Posted by evenstar7139 View Post
    What happened right here. I just saw this in a PDO tutorial and I'm not sure what's going on with the $STH variable:

    PHP Code:
    $STH $DBH->prepare("INSERT INTO folks ( first_name ) values ( 'Cathy' )");  
    $STH->execute(); 
    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

    eBookworm.us

  13. #13
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    Quote Originally Posted by NogDog View Post
    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.

    Quote Originally Posted by NogDog View Post
    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. :)

    Anyhoo, let's see if I get this.

    PHP Code:
    $STH $DBH->prepare("INSERT INTO folks ( first_name ) values ( 'Cathy' )");  
    $STH->execute(); 
    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->setAttributePDO::ATTR_ERRMODEPDO::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):
    mysql:host=myhost;dbname=mydatabase, myusername, mypassword

    SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)012
    I don't know how it can say it's not getting the username, or password, or anything. I see them right there in the string?
    Last edited by evenstar7139; 07-30-2012 at 05:57 PM.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  14. #14
    Join Date
    Jul 2012
    Location
    Vancouver
    Posts
    61
    PHP Code:
       $connectString="mysql:host={$this->dbHost};dbname={$this->dbName};";
                     
    $this->DBH = new PDO($connectString$this->dbUser$this->dbPass); 
    Try that.
    Last edited by mistin.ca; 07-30-2012 at 06:07 PM.

  15. #15
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    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->setAttributePDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION ); //this is line 22
       

    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles