Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Sciences and Social Sciences HOPE PHP & MySQL Stewart Blakeway FML 213

Similar presentations


Presentation on theme: "Faculty of Sciences and Social Sciences HOPE PHP & MySQL Stewart Blakeway FML 213"— Presentation transcript:

1 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE PHP & MySQL Stewart Blakeway FML 213 blakews@hope.ac.uk

2 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What will we cover Basic SQL commands – Inserting Records – Selecting Records – Filtering Records Some formatting of controls and displaying of data

3 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Why As part of your assessment you will be expected to 1.Create a link to the SQL Server 2.Select a database 3.Talk to the database (retrieve, add, append, etc)

4 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Data Types MySQL uses all the standard numeric data types. Some you will have come across – INT-2147483648 to 2147483647 – TINYING-128 to 127 or 0 to 255 – SMALLINT-32768 to 32767 or 0 to 65535 – MEDIUMINT-8388608 to 8388607 or 0 to 16777215 – BIGINT-9223372036854775808 to 9223372036854775807 or 0 to 18446744073709551615 – FLOAT-10,2 up to 24 places – DOUBLE-16,4 up to 53 places

5 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Date and Time Types DATE- A date in YYYY-MM-DD format DATETIME- A combination of both the date and the time, in YYYY-MM-DD & HH:MM:SS TIMESTAMP- Similar to DATETIME with hyphens an colons omitted TIME- A time in format HH:MM:SS YEAR(x)- The year, the length is specified by x.

6 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE String Types CHAR (x)Fixed length VARCHAR (x)Variable length BLOB or TEXTMax of 65535 chars TINYBLOB or Max of 255 chars TINYTEXT MEDIUMBLOB or Max of 255 chars MEDIUMTEXT LONGBLOB or Max of 255 chars LONGTEXT ENUMA list with a max 65535 values

7 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE MySQL In order to complete your assignment you will need to create a database The database will be empty, the structure you will need to define The database and the tables should be named appropriately The fields within the database should be of a suitable type

8 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Talking to the DATBASE 1.Create a connection with the SQL Server 2.Select the appropriate database 3.Construct the SQL Query 4.Execute the SQL Query Have you created the DATABASE yet? Although you could use SQL statements to create the database and the tables within the database, a utility called phpMyAdmin offers an GUI to do the same task.

9 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1. The Connection mysql_connect($host,$user,$password) host is the SQL server address user is the username to login to the database password is the password associated with the username

10 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1. The Connection Syntax Storing and testing the connection $conn = mysql_connect (“localhost",“root","") or die(mysql_error()); echo $conn; Should echo out: Resource id #1 or Resource id #2

11 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE http://hopelive.hope.ac.uk /computing/ 2. Selecting the database There could be many databases on the SQL server. You need to ensure you select the correct one: mysql_select_db(database,connection) database is the name of the database connection is the connection parameters to connect to the SQL server as seen in the previous slide

12 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 2. Selecting the database Syntax There could be many databases on the SQL server. You need to ensure you select the correct one: mysql_select_db(“student",$conn) or die(mysql_error());

13 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 3. Constructing the SQL Query To interact with the database you construct standard MYSQL statements $sql = “CREATE TABLE tablename ( fieldname1 datatype, fieldname2 datatype )”;

14 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 4. Executing the SQL Query $sql = "CREATE TABLE student( studentId int not null primary key, surname varchar (25), firstname varchar (25) )"; mysql_query ($sql, $conn); String Variable PHP command to run the SQL The variable that contains the SQL The variable that contains the connection settings

15 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Retrieving Error Messages To ensure that the SQL works you can retrieve error messages and display them! $result = mysql_query ($sql, $conn) or die(mysql_error()); echo $result; die is optional for all mysql statements As is mysql_error

16 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE http://hopelive.hope.ac.uk /computing/ So far! We have connected to the SQL Server We have selected the correct database We have created a table called student with 3 fields. studentId surname firstname

17 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE All together $conn = mysql_connect (“localhost",“root",""); mysql_select_db(“student",$conn); $sql = "CREATE TABLE student( studentId int not null primary key, surname varchar (25), firstname varchar (25) )"; mysql_query ($sql, $conn);

18 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Inserting DATA The next step is to insert data into your table. $sql = "INSERT INTO student VALUES ( ‘100100100’, ‘Blakeway’, ‘Stewart’ )"; mysql_query ($sql,$conn) or die(mysql_error());

19 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Unlikely! It is very unlikely that you will enter predetermined data into a database This is very useful for testing purposes More likely you will need to obtain the data via the user How do we achieve this?

20 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 2 Methods Create the form in html and send the data to another file – we have all been doing this already Create the form in php and send the data to the same file – it is easier to work with one file rather than switching between two

21 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE http://hopelive.hope.ac.uk /computing/ if ($_POST[viewed] != "yes") { echo “ ”; } else { } Alternative method. Both the get data and the do data are contained within the one file

22 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Using Forms if ($_POST[op]!="viewed") { echo " Student ID Number Student Forename Student Surname "; }

23 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Processing data from forms else // if it has then add the data { //set sql string using values as indicated by the form $sql = "INSERT INTO tblstudent VALUES ( '$_POST[studID]', '$_POST[studSN]', '$_POST[studFN]')"; if (mysql_query($sql,$conn)) // is it valid? { echo "Record Added!"; } else // if not { echo "Record not Added!"; } Why not use die(mysql_error()) ? Can you spot the error?

24 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Retrieving Data At some point you will want to retrieve the data from the database. Immediately after you insert data for the first time would be a good time! $sql = "SELECT * FROM student“; $result = mysql_query ($sql,$conn); Where does the data go? Into a RESOURCE

25 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Getting the data from the RESOURCE $newArray = mysql_fetch_array($result); $newArray = array ( “studentId” => “05004343”, “surname” => “Blakeway”, “firstname” => “Stewart” ) The RESOURCE PHP Function to fetch data from the RESOURCE

26 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Moving through multiple records Dealing with more than one record! while ($newArray = mysql_fetch_array($result)) { $sn = $newArray[‘surname’]; echo $sn. “ ”; }

27 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Combo Boxes As you can see from the last slide each surname is displayed on screen What if you wanted to put the names in a big list for the user to select one? This is fairly straightforward once it has been thought through

28 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Overall structure of the page! if ($_POST[viewed] != "yes") { // display the form // allow user to choose a record } else { // display what was selected }

29 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Forms! How many surnames will there be?

30 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 3 Steps 1.First bit of the form 2.THE LOOPING (SURNAMES or whatever) 3.The last bit of the form

31 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The first bit echo " Select a Record to View -- Select One -- ";

32 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The complicated bit while ($newArray = mysql_fetch_array($result)) { $sn = $newArray[‘surname’]; echo " $sn "; }

33 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The end bit echo =" ";

34 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Overall structure of the page! if ($_POST[viewed] != "yes") { // display the form } else { // display what was selected }

35 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Displaying the specific record or the ELSE section 1.Create a connection to the SQL Server 2.Select the required database 3.Construct the SQL 4.Execute the SQL 5.Extract the required data 6.Display the record

36 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 3. Constructing the SQL Now we have specific criteria for data that needs to be SELECTED from our record set $sql = "SELECT * FROM student"; $sql = "SELECT * FROM student WHERE surname = '$_POST[sel_SN]'";

37 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 4. Executing the SQL $result = mysql_query ($sql, $conn); A variable to hold the results of executing the SQL The PHP command to execute an SQL statement The variable containing the connection settings The variable containing SQL statement

38 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 5. Extracting the required data from the results while ($newArray = mysql_fetch_array($result)) { $studID = $newArray[‘studentId’]; $studFN = $newArray[‘firstname’]; $studSN = $newArray[‘surname’]; }

39 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 6. Displaying the Record echo $studFN.$studSN.$studID; or echo “ Id Number Forename s urname $studID $studFN $studSN ”;

40 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What have we covered? 1.Create a connection to the SQL Server 2.Select the required database 3.Construct the SQL 4.Execute the SQL 5.Extract the required data 6.Display the record

41 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Class Exercise (time permitting) Create a function called nowAdd that will add the data obtained from a form to a database called dbCourse. Posted name attributes are: fName, sName Database table is: tblStudent SQL server is located at: localhost SQL Username is: tutor SQL Password is: mypw

42 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Spot the errors $sql = “CREATE TABLE tablename ( fieldname1 varchar(30), fieldname2 varchar(30), )

43 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Spot the errors while ($newArray == mysql_fetch($result)) { $studID => $newArray[‘studentID’]; $studFN => $newArray[‘firstname’]; $studSN => $newArray[‘surname’]; };

44 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What next? Independent Study: http://www.w3schools.com/sql/default.asp Begin database design and implementation for assessment

45 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Any Questions?


Download ppt "Faculty of Sciences and Social Sciences HOPE PHP & MySQL Stewart Blakeway FML 213"

Similar presentations


Ads by Google