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

Slides:



Advertisements
Similar presentations
PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.
Advertisements

PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
Session 2Introduction to Database Technology Data Types and Table Creation.
MySQL. To start go to Login details: login: labuser password:macimd15 – There.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap.
Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands.
Faculty of Sciences and Social Sciences HOPE Different Users and Uploading Files Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE User Sessions & The Include Statement Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Uploading Resources Stewart Blakeway FML 213
Objectives Connect to MySQL from PHP
Intermediate PHP & MySQL
PHP and MySQL Web Development tMyn1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
ADVM420- Class #4 Web Design with PHP and MySQL Adding and Listing from a MySQL Database.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
Session 5: Working with MySQL iNET Academy Open Source Web Development.
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
ASP.NET Programming with C# and SQL Server First Edition
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
PHP MySQL Introduction
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
INTERNET APPLICATION DEVELOPMENT PRACTICAL ON CONNECTING TO MYSQL.
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Dbwebsites 2.1 Making Database backed Websites Session 2 The SQL… Where do we put the data?
PHP meets MySQL.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
Installing and Using MySQL and phpMyAdmin. Last Time... Installing Apache server Installing PHP Running basic PHP scripts on the server Not necessary.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
MySQL Databases & PHP Integration Using PHP to write data to, and retrieve data from, a MySQL database.
CSC 2720 Building Web Applications Database and SQL.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Web-Based Database Programming with PHP. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn PHP Basics PHP functions –To.
Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
PHP Database Processing CIS 1715 Web Technologies.
Advanced Web 2012 Lecture 3 Sean Costain What is a Database? Sean Costain 2012 A database is a structured way of dealing with structured information.
Controlling Web Site Access Using Logins CS 320. Basic Approach HTML form a php page that collects the username and password  Sends them to second PHP.
PHP getting data from a MySQL database. Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Sql DDL queries CS 260 Database Systems.
MySQL. Is a SQL (Structured Query Language) database server. Can be accessed using PHP with embedded SQL Queries Supports Large DB’s, 60,000 tables with.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Class 3Intro to Databases Class 4 Simple Example of a Database We’re going to build a simple example of a database, which will allow us to register users.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Intro to MySQL.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
SQL and SQL*Plus Interaction
Storing Images Connect to the server using the correct username and password. $conn = mysql_connect(“yourserver”, “joeuser”, “yourpass”); Create the database.
Database systems Lecture 2 – Data Types
CS3220 Web and Internet Programming SQL and MySQL
MySQL Database System Installation Overview SQL summary
Presentation transcript:

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

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

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)

Faculty of Sciences and Social Sciences HOPE Data Types MySQL uses all the standard numeric data types. Some you will have come across – INT to – TINYING-128 to 127 or 0 to 255 – SMALLINT to or 0 to – MEDIUMINT to or 0 to – BIGINT to or 0 to – FLOAT-10,2 up to 24 places – DOUBLE-16,4 up to 53 places

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.

Faculty of Sciences and Social Sciences HOPE String Types CHAR (x)Fixed length VARCHAR (x)Variable length BLOB or TEXTMax of 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 values

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

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.

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

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

Faculty of Sciences and Social Sciences HOPE /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

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());

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 )”;

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

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

Faculty of Sciences and Social Sciences HOPE /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

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);

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

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?

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

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

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

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?

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

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

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. “ ”; }

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

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 }

Faculty of Sciences and Social Sciences HOPE Forms! How many surnames will there be?

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

Faculty of Sciences and Social Sciences HOPE The first bit echo " Select a Record to View -- Select One -- ";

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

Faculty of Sciences and Social Sciences HOPE The end bit echo =" ";

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

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

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]'";

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

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’]; }

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 ”;

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

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

Faculty of Sciences and Social Sciences HOPE Spot the errors $sql = “CREATE TABLE tablename ( fieldname1 varchar(30), fieldname2 varchar(30), )

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

Faculty of Sciences and Social Sciences HOPE What next? Independent Study: Begin database design and implementation for assessment

Faculty of Sciences and Social Sciences HOPE Any Questions?