login class using MVC approach

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semanticnotion
    New Member
    • Sep 2010
    • 66

    #16
    Ooh thats great...!
    It works fine Thanks kovik thanks very much if you are on my back i will be the php master soon.
    hope you will help me in future.

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #17
      No problem. :)

      Comment

      • semanticnotion
        New Member
        • Sep 2010
        • 66

        #18
        ok kovik if i want to retrieve the data in html table from admin e.g

        ID NAME PASSWORD
        1 abc *******

        then how can i call this from view to controller and from controller to model remember that the select query should be in model class and data will be displayed on html page in view class.

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #19
          The way that MVC works is that your Controller decides which Model to use and which View to use, based on user input. On the internet, "user input" is generally simplified down to the selected URL.

          The Controller chooses the model which has the data it needs, and it choses the View that displays that data the way it needs to be displayed. Then, it injects the data into the view and outputs the result.

          You'd want a Model class that creates an array of data from the database data, and a View class that uses that data to make your HTML table.

          Comment

          • semanticnotion
            New Member
            • Sep 2010
            • 66

            #20
            i do this in my html

            Code:
            $rs=new SelectSubject();
            
            $select=$rs->get();
            
             //$select=mysql_query("select * from subject") or die(mysql_error());
            
                      
            
            while($rs=mysql_fetch_array($select))
            {
            then in controller like this

            Code:
             class SelectSubject
                {
                    function get()
                    {
                        $selectsubject=new LoginSystem();
            
                        $selectsubject->getSubject();
                    }
            and in model

            Code:
            function getSubject()
                {
                    $this->connect();
                    $sql=sprintf("select * from subject") or die(mysql_error());
                    $result=mysql_query($sql);
                    return $result;
                }

            this give me the error
            Code:
            Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /var/www/test/view/add_subject.php on line 112 Call Stack: 0.0007 338948 1. {main}() /var/www/test/view/add_subject.php:0 0.0215 397500 2. mysql_fetch_array() /var/www/test/view/add_subject.php:112

            code of line 112 is
            Code:
            while($rs=mysql_fetch_array($select))

            Comment

            • kovik
              Recognized Expert Top Contributor
              • Jun 2007
              • 1044

              #21
              That just means that $select is not a resource. You can verify this by echoing get_type(). If it's not a result, then you are probably getting a MySQL error. However, you don't handle it correctly.

              This:
              Code:
              $sql=sprintf("select * from subject") or die(mysql_error());
              $result=mysql_query($sql);
              Should be this:
              Code:
              $sql=sprintf("select * from subject");
              $result=mysql_query($sql)or die(mysql_error());
              Also, the call to sprintf() is unnecessary.

              Comment

              • semanticnotion
                New Member
                • Sep 2010
                • 66

                #22
                kovik i do the changes but still not working is there any other possibility to call from to controller and then model but the data should be displayed in view and query should be in model.

                Comment

                • kovik
                  Recognized Expert Top Contributor
                  • Jun 2007
                  • 1044

                  #23
                  What errors are you getting?

                  Comment

                  • semanticnotion
                    New Member
                    • Sep 2010
                    • 66

                    #24
                    there is no error but the data is not displayed in html table.
                    insert query is working fine but select does't work
                    i change the code but still not working plz help whats wrong with this code.

                    Comment

                    • semanticnotion
                      New Member
                      • Sep 2010
                      • 66

                      #25
                      Notice: Undefined variable: dbhost in /var/www/test/model/LoginSystem.cla ss.php on line 31 Call Stack: 0.0006 341820 1. {main}() /var/www/test/view/add_subject.php :0 0.1268 402776 2. SelectSubject->get() /var/www/test/view/add_subject.php :105 0.1268 403536 3. LoginSystem->LoginSystem( ) /var/www/test/controller/check_login.php :75 Notice: Undefined variable: dbname in /var/www/test/model/LoginSystem.cla ss.php on line 32 Call Stack: 0.0006 341820 1. {main}() /var/www/test/view/add_subject.php :0 0.1268 402776 2. SelectSubject->get() /var/www/test/view/add_subject.php :105 0.1268 403536 3. LoginSystem->LoginSystem( ) /var/www/test/controller/check_login.php :75 Notice: Undefined variable: dbuser in /var/www/test/model/LoginSystem.cla ss.php on line 33 Call Stack: 0.0006 341820 1. {main}() /var/www/test/view/add_subject.php :0 0.1268 402776 2. SelectSubject->get() /var/www/test/view/add_subject.php :105 0.1268 403536 3. LoginSystem->LoginSystem( ) /var/www/test/controller/check_login.php :75 Notice: Undefined variable: dbpassword in /var/www/test/model/LoginSystem.cla ss.php on line 34 Call Stack: 0.0006 341820 1. {main}() /var/www/test/view/add_subject.php :0 0.1268 402776 2. SelectSubject->get() /var/www/test/view/add_subject.php :105 0.1268 403536 3. LoginSystem->LoginSystem( ) /var/www/test/controller/check_login.php :75 Warning: mysql_connect() : Access denied for user 'www-data'@'localhos t' (using password: NO) in /var/www/test/model/LoginSystem.cla ss.php on line 91 Call Stack: 0.0006 341820 1. {main}() /var/www/test/view/add_subject.php :0 0.1268 402776 2. SelectSubject->get() /var/www/test/view/add_subject.php :105 0.1287 403536 3. LoginSystem->getSubject() /var/www/test/controller/check_login.php :77 0.1287 403536 4. LoginSystem->connect() /var/www/test/model/LoginSystem.cla ss.php:180 0.1287 403712 5. mysql_connect() /var/www/test/model/LoginSystem.cla ss.php:91 Unable to connect to MySQL

                      Comment

                      • semanticnotion
                        New Member
                        • Sep 2010
                        • 66

                        #26
                        3 days ago it gives me the error of not valid resource and now its give me the above error

                        Comment

                        • kovik
                          Recognized Expert Top Contributor
                          • Jun 2007
                          • 1044

                          #27
                          The error says "Undefined variable: dbhost". The only time that you reference "$dbhost" is in your constructor for the LoginSystem class. It is never defined. Did you mean to include it in your constructor's declaration?

                          Instead of:
                          Code:
                          function LoginSystem()
                              {
                                  require_once('settings.php');
                           
                                  $this->db_host = $dbhost;
                                  $this->db_name = $dbname;
                                  $this->db_user = $dbuser;
                                  $this->db_password = $dbpassword;
                              }
                          Try:
                          Code:
                          function LoginSystem($dbhost, $dbname, $dbuser, $dbpassword)
                              {
                                  require_once('settings.php');
                           
                                  $this->db_host = $dbhost;
                                  $this->db_name = $dbname;
                                  $this->db_user = $dbuser;
                                  $this->db_password = $dbpassword;
                              }
                          And be sure to send this variables when creating instances of the class. If those are supposed to be defined in "settings.p hp", then I'd say that this is a fairly bad design, but eh.

                          Comment

                          • semanticnotion
                            New Member
                            • Sep 2010
                            • 66

                            #28
                            ya my setting.php contain "localhost","us er","password", "db_name" i comment that and give variable values in loginsystem class.
                            now
                            Unable to connect to MySQL

                            Comment

                            • semanticnotion
                              New Member
                              • Sep 2010
                              • 66

                              #29
                              i changed the constructor like this
                              Code:
                               function LoginSystem()
                              	{
                              		//require_once('settings.php');
                              		
                              		$this->db_host ="localhost";                            //$dbhost;
                              		$this->db_name = "kims";                                //$dbname;
                              		$this->db_user = "root";                                //$dbuser;
                              		$this->db_password = "root";                            //$dbpassword;
                              	}
                              and it gives me no error but still data is not displayed in html table i thing i return something wrong in my controller and model.

                              Comment

                              • kovik
                                Recognized Expert Top Contributor
                                • Jun 2007
                                • 1044

                                #30
                                Have you tried connecting directly using those credentials? They are likely incorrect if you can't connect.

                                Comment

                                Working...