Blog 10

Class Notes:

Today we looked at Milestone 2 which connects to a MySQL database using PHP website.

Milestone is moving from the initial analysis to more detail about our  project. We will extend and develop the website, we will add the extra characteristic of a competition page accessible from the home page.

This competition page will be a form which the user has to fill out for schools to win a computer. This competition page will be accessible from the home page via a graphic link.

This competition page will have fields to be filled out and competition question with radio-buttons answers. The competition page will have data retrieved (from DB) and data submitted to DB.

The competition question will be about the website so they have to go through the website.

Before the data is submitted a question must be retrieved from the database to be displayed on the form.

Because when the form is first displayed the question (is a random question from a database set of questions, i.e. competition is generated and extracted from the DB), and this will be displayed on the form view which makes the competition page a active view.

The database will have 5 tables in a relational database.

After filling in the competition form then there will be a thank you message which will be displayed (this thank you message will be coming from the database).

We will build a controller (dealing with form, can be same existing fat controller, or a second controller), model and view (the form is treated as a view).

Milestone 2 will have a new chapter in the IA report describing how you are building the website. Cite code from other sources.

This is the php code we used to connect to a ‘food’ database we created using phpmyadmin:

<?php
    
    class DBconnection {
        private $rs; // Procedural "handle" or "resource" to database
        private $connectRs;
        
        private function connectDb($pStrDatabase)
        {
            $this->connectRs = mysql_connect("localhost","root","");
            if(!$this->connectRs)
            {
                echo "Error connecting to the database server".mysql_error($this->connectRs);
                $this->connectRs = -1;
            }
            else
                echo "Connected </br>";
            $dbRs = mysql_select_db($pStrDatabase,$this->connectRs);
            if(! $dbRs)
            {
                echo "Error selecting the database".mysql_error($this->connectRs);
                
            }
            else
                echo "Selected ".$pStrDatabase."</br>";
        }
    
        public function query($pStrSQL)
        {
        
            $this->rs = -1;// BAD RECORDSET
        
            $this->rs = mysql_query($pStrSQL,$this->connectRs);
            if( !$this->rs)
            {
                echo "Error running query [$pStrSQL] ".mysql_error($this->connectRs)."<br>";
                $this->rs = -1;
            
            }
        
        
        }
    
        public function DBConnection($pStrDatabase)
        {
            
            $this->connectDb($pStrDatabase);
            
        }
        
        public function next(){
            $aRow = mysql_fetch_assoc($this->rs);
            return $aRow;
        }
        
        public function free(){
            mysql_free_result($this->rs);
        }
    }// end of database class
   
    // Test Code using Food database
    $aDB = new DBConnection("Food");
    $aDB->query("INSERT INTO `Ingredient` (`Name`, `Description`) VALUES ('GARLIC', 'Smelly')");
    $aDB->query("SELECT * FROM INGREDIENT");
    
    while($aRow = $aDB->next()){
        echo $aRow['Name']."  ".$aRow['Description']."</br>";
    }
    $aDB->free();
    $aDB->query("DELETE FROM INGREDIENT WHERE `Name` = 'GARLIC'");
    
    
    
?>

I went through and commented this code. But when I ran the code it threw an error because i have php version 7 which considers mysql deprecated and uses mysqli (which is OO version of mysql) so I had to write i at end of mysql in the above code and change the query parameter order around so the connection was first followed by the query,, then after I ran the code it worked successfully.

 

Project Notes:

I will start working on milestone two.

Leave a comment