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.

Slides:



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

JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
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.
PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
PHP and MySQL Database. Connecting to MySQL Note: you need to make sure that you have MySQL software properly installed on your computer before you attempt.
NMD202 Web Scripting Week5. What we will cover today PHPmyAdmin Debugging – using print_r Modifying Data PHP (cont.) 4D Methodology File and IO operations.
Faculty of Sciences and Social Sciences HOPE PHP & MySQL Stewart Blakeway FML 213
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
Objectives Connect to MySQL from PHP
Multiple Tiers in Action
U:/msu/course/cse/103 Day 23, Slide 1 Review of Day 22 What query did you use to search for an actor by name? –Return matches.
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.
PHP Scripts HTML Forms Two-tier Software Architecture PHP Tools.
PHP and SQL Server: Queries IST2101. Project Report 4 SQL Queries Due Sunday, 4/5 at 11:59pm Instructions on how to access team webspace and SQL database.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Deleting and Updating Records in MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
1 Insert, Update and Delete Queries. 2 Return to you Address Book database. Insert a record.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Recruitment Office Procedures Job Posting Requests Creating a Search Committee –Adding Search Committee MembersAdding Search Committee Members –Designating.
Class 3 MySQL Robert Mudge Reference:
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:
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
Tutorial 10 Adding Spry Elements and Database Functionality Dreamweaver CS3 Tutorial 101.
Introducing Access Lesson 1. Objectives Start Access and explore the Database window Explore database objects Enter, edit, and delete records in a datasheet.
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 MySQL and phpMyAdmin. 2 Navigate to and log on (username: pmadmin)
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
PHP meets MySQL.
_______________________________________________________________________________________________________________ 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.
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.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
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)
PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)
Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
>> PHP: MySQL & CRUD. R ecall Database Tables Records is composed of Operations (CRUD) Create Retrieve Update Delete DBMS Access Control MySQL phpMyAdmin.
MySQL MySQL and PHP – interacting with a database.
MYSQL AND MYSQL WORKBENCH MIS2502 Data Analytics.
Education And Training CTC IT DIVISION PivotLink User Training April 2010.
Module of the week Webform 7.x-4.0-alpha9 – admin/config/content/webform Add content types, choose allowed fields, default values and advanced options.
1.Switch on the computer and wait for loading. 2.Select the Windows 7 OS at the end of the list. 3.Click on the link ‘Administrator’ 4.Enter the administrator.
1.Switch on the computer and wait for loading. 2.Select the Windows 7 OS at the end of the list. 3.Click on the link ‘Administrator’ 4.Enter the administrator.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
A little PHP. Enter the simple HTML code seen below.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Database application MySQL Database and PhpMyAdmin
PHPMyAdmin.
MySQL and PHPMyAdmin 1.
Introduction to Web programming
Presentation transcript:

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 file. Now we would like to get the information out of the MySQL database. PHP must connect to the database, perform a SQL select statement of the appropriate table or tables and then parse the query result and display the information.

Back to the sign-up-for-training form

Code to load database information into select element (drop-down list)

Information needed to connect to database $host = "localhost"; //if PHP server and mySQL server same $user="blum2"; $password = “tb4db"; $database = "blum2"; Even though the client will never see this code, it is standard procedure to place username and password data eventually gets placed in another file.

Code to connect to MySQL $dbc = mysql_connect($host,$user,$password); if($dbc==false) { die("Problem connecting to MySQL."); //The die() function prints a message //and exits the current script. }

Code to choose a database $db = mysql_select_db($database, $dbc); if($db==false) { die("problem with database."); }

Code to ask for data from Session table $sql = "SELECT SessionID, SubjectID, LocationID, Date, Time FROM Session"; //$sql = "SELECT * FROM Session"; $result = mysql_query($sql,$dbc); The $sql variable above will hold a string corresponding to a SQL query requesting data from the Session table. The last statement performs the SQL query and places the results in a variable result.

Displaying the results in the drop-down list while($row = mysql_fetch_array($result, MYSQL_NUM)) { print " $row[1]: $row[3] $row[4] "; } This code loops through the records resulting from the query. For each record, row becomes an array of the fields of that record. The order of fields in row matches the order in the SQL statement.

Result in browser We need to re-insert our code that only displayed present and future training sessions. It would be preferable if the

Code to display only future training while($row = mysql_fetch_array($result, MYSQL_NUM)) { list($year, $month, $day) = split('[/.-]', $row[3]); //note info from database arranges it year, month, day //month already a number $trainingTime = mktime(0,0,0,$month, $day+1, $year); $now = time(); if($trainingTime >= $now) { print " $row[1]: $row[3] $row[4] "; }

The date format has changed – $year comes first and $month is a number list($year, $month, $day) = split('[/.-]', $row[3]); //note info from database arranges it year, month, day //month already a number //same as before $trainingTime = mktime(0,0,0,$month, $day+1, $year); $now = time(); if($trainingTime >= $now)

Result in browser

Return to phpmyadmin, click on your database and the Query

Choose the tables to be used in the query, then start choosing the fields

Choose SubjectTitle from SubjectMatter instead of SubjectID from Session Also choose to add additional fields to the query.

Make a “natural join” between the Session and SubjectMatter tables Note that we have chosen to display all of the fields except the last, and on the last field SubjectMatter.SubjectID we impose the constraint that its value equal that from Session.SubjectID -- that is we want the records from the two tables to have the proper relationship. The character ` seen above is above the Tab on the upper left of the keyboard.

Update the query, then Submit the query

Results of Query Be careful to use the phpmyadmin’s navigation, such as Edit, and not the browser’s back button if you need to fix anything. If happy click Create PHP Code Query results

The phpmyadmin generated PHP code for the query Copy the code over to your PHP script.

Join query code pasted into PHP script

Page with Subject titles instead of Subject IDs