Web Programming Language

Slides:



Advertisements
Similar presentations
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.
Advertisements

CC SQL Utilities.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
What is MySQL? MySQL is a database. The data in MySQL is stored in database objects called tables. A table is a collections of related data entries and.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Web Application Development. Tools to create a simple web- editable database QSEE MySQL (or PHPMyAdmin) PHP TableEditor.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
 Mysql – popular open-source database management system  PHP usually works with Mysql for web- based database applications  LAMP applications—Web-based.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
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.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
PHP Part 2.
Database and mySQL Week 07 Dynamic Web TCNJ Jean Chu.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
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.
2010/11 : [1]PHP with MySQLBuilding Web Applications using MySQL and PHP (W1) PHP with MySQL.
Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
PHP and Mysql Database. PHP and Database Mysql – popular open-source database management system PHP usually works with Mysql for web-based database applications.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
PHP: MySQL. PHP Connect to MySQL PHP 5 and later can work with a MySQL database using: – MySQLi extension (the "i" stands for improved) – PDO (PHP Data.
PHP Database Pemrograman Internet. PHP MySQL Database With PHP, you can connect to and manipulate databases. MySQL is the most popular database system.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CHAPTER 10 PHP MySQL Database
CSC 2720 Building Web Applications Accessing MySQL from PHP.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
13 – PHP MySQL Connection Informatics Department Parahyangan Catholic University.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
Web Systems & Technologies
Tried my best to simplify it for you!
3 A Guide to MySQL.
PHP: MySQL Lecture 14 Kanida Sinmai
Web Systems & Technologies
Introduction to Dynamic Web Programming
SQL and SQL*Plus Interaction
Open Source Server Side Scripting Permissions & Users
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Database application MySQL Database and PhpMyAdmin
Introduction to Web programming
JDBC.
Using SQL*Plus.
PHP-language, database-programming
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter 8 Working with Databases and MySQL
MySQL Web Application Connecting to a MySQL database
PHP and MySQL.
Structured Query Language
PHP: Database Basic Selection FdSc Module 109
Introduction To Structured Query Language (SQL)
Chapter 2: Creating And Modifying Database Tables
Tutorial 6 PHP & MySQL Li Xu
Using SQL*Plus.
PHP and MySQL.
MySQL Database System Installation Overview SQL summary
MySQL Web Application Connecting to a MySQL database
PHP Forms and Databases.
Introduction to Web programming
Presentation transcript:

Web Programming Language Chapter 6

PHP Files & MySQL Databases PHP and Files Introducing MySQL and phpMyAdmin Structured Query Language MySQLi

PHP & Files PHP can be used to work with the server’s filesystem. It can open and close files, read, write and append to them. <?php $file =fopen("test.txt", "w"); fwrite($file, "Hello World"); fclose($file); ?> This example would open a file called ‘test.txt’ and write “Hello World” to it. Note the “w” parameter in the fopen() function, indicating it is open in write mode.

PHP File Open Modes Mode Description r Read only. Starting at the beginning of the file r+ Read / Write. Starting at the beginning of the file w Write only. Clears the file if it isn’t empty, or creates a new file if it doesn’t exist w+ Read / Write. Clears the file if it isn’t empty, or creates a new file if it doesn’t exist a Append. Writes to the end of a file, or creates a new file if it doesn’t exist a+ Read / Append. Keeps file content and writes to the end of the file x Write only. Creates a new file, and returns an error if the file exists x+ Read / Write. Creates a new file, and returns an error if the file exists

PHP – Reading from a file Using fread() to read the whole file. <?php $file =fopen("test.txt", "r"); $content = fread($file, filesize(“test.txt”)); fclose($file); ?> But perhaps if the file is v. large, you might do it line by line. <?php $file =fopen("test.txt", "r"); while(!feof($file)) { echo fgets($file) . “<br>”; } fclose($file); ?> Not file end of file Get a line

File Access Permissions PHP has several functions for managing directories mkdir() – Makes a new directory rmdir() – Deletes a directory chmod() – Changes permissions on a directory To create a directory without any access restrictions (not a good idea!), use this; <?php mkdir(“newfolder”, 0777); ?> Note the 2nd parameter sets the access permissions

File Access Permissions Access Permissions consist of 4 numbers:- The first number is always 0 The second number is for permissions for the owner The third number is for the owner’s user group The fourth number is for everybody else There are a variety of combinations:- 1 = execute permissions 2 = write permissions 4 = read permissions Common combinations are:- 0644 – Read and write for the owner, but only read for anyone else 0755 – Everything for the owner, but read and execute for everyone else

More PHP File Functions scandir() – returns an array of the contents of a directory is_dir() – returns a Boolean as to whether something is a directory is_file() – returns a Boolean as to whether something is a file is_executable() – returns whether the file is an executable file file_exists() – returns whether the file exists copy() – takes two parameters, the source and destination of a file

MySQL Files are ok… but databases are often a better way of managing data! Just imagine saving calendar appointments in a file, and then searching that file to find which appointments you need to display! MySQL is a database server that scales for small and large applications using SQL (Structured Query Language) to interact with it. Data is stored in tables – tables store data about entities Tables consist of rows and columns, (like spreadsheets) Tables can be related to each other In other courses you will learn about databases in depth, here we will look at key operations.

PHPmyadmin A GUI (Graphical User Interface) allowing easy database creation and manipulation.

Create a Database

Create a Table

Set up the columns To store appointments, you might want an id, a date, a title, and details. Auto Increment Integer Variable Character Text (Stored Separately)

Check the Structure

Structured Query Language (SQL) Database language used by a variety of databases SELECT – for retrieving data from the database SELECT * FROM appointments LIMIT 0 , 30; The general syntax of a SELECT statement is as follows. SELECT column_name1, column_name2 FROM table_name; A WHERE clause can be added for more precise results. SELECT * FROM appointments WHERE id=1;

SQL - INSERT Basic Syntax Specifying which columns Example INSERT INTO table_name VALUES (value1, value2,…); Specifying which columns INSERT INTO table_name (column1, column2,…) VALUES (value1, value2,…); Example INSERT INTO appointments (date, title, details) VALUES ("2016-08-05", "Meeting", "All day in room 401");

SQL INSERT

SQL UPDATE Basic Syntax Example UPDATE table_name SET column1=value1, column2=value2 WHERE column=value; Example UPDATE appointments SET details="Meet at 9AM" WHERE id = 1;

SQL UPDATE

SQL DELETE Basic Syntax Example DELETE FROM table_name WHERE column_name = value; Example DELETE FROM appointments WHERE date = "2016-08-05";

MySQLi MySQLi (the i is for ‘improved’) is an API for manipulating a MySQL database using PHP. Be careful to use MySQLi, NOT MySQL! There are alternative to MySQLi, but we will stick with MySQLi MySQLi offers either a procedural approach, or an object oriented approach. We will explore both – it is your choice which to use – perhaps the OO option?

Connecting to the Database Procedural $link = mysqli_connect(“localhost",“username",“password",“databasename") or die("Error" . mysqli_error($link)); //Some Code mysqli_close($link); OO $mysqli = new mysqli("localhost", “username", "password", “databasename"); // Some Code $mysqli->close(); Worth moving the connection script to a separate file Include “database.php”

Querying a Database Create a variable that stores the query $qry1 = “CREATE DATABASE calendar”; $qry2 = "CREATE TABLE users ( FirstName varchar(15), LastName varchar(15) )”; $qry3 = “INSERT INTO appointments (date, title, details) VALUES (‘2016-08-06’, ‘Exam’, ‘9AM’)”; Use mysqli to call the query mysqli_query($link, $qry1); //procedural approach $mysqli->query($qry3); // OO approach

Tip If you have a mistake in your SQL, you may get no response, and no error. Use the SQL option in phpMyAdmin to check and fix an error.

PHP and SELECT The SELECT statement returns some results – we could check how many… Procedural $link = mysqli_connect(“localhost",“username",“password",“databasename"); $qry = “SELECT * FROM appointments”; if ($result = mysqli_query($link, $qry)) {     echo “There are “ . mysqli_num_rows($result) . “ rows!”;     mysqli_free_result($result); } mysqli_close($link); OO $mysqli = new mysqli("localhost", “username", "password", “databasename"); $qry = “SELECT * FROM appointments”; if ($result = $mysqli->query($qry)) {      echo “There are “ . $result->num_rows . “ rows”;     $result->close(); } $mysqli->close();

PHP and SELECT The results are stored in a data object, and they can retrieved in different formats. As an array $qry = “SELECT * FROM appointments”; $result = $mysqli->query($qry); while($row = $result->fetch_array()) { print_r($row); }

PHP and SELECT As an associative array $qry = “SELECT * FROM appointments”; $result = $mysqli->query($qry); while($row = $result->fetch_assoc()) { print_r($row); }

PHP and SELECT As an object $qry = “SELECT * FROM appointments”; $result = $mysqli->query($qry); while($row = $result->fetch_object()) { print_r($row); }

Key Points Because PHP is code running on the server, it can be used to read and write data from files stored on the server and there are several functions to help with that. There are different modes when opening a file, depending on whether you intend to read, write or append the file. Appropriate permissions should be set to limit access to files stored on the server. MySQL is a database server that is a key part of the WAMP / LAMP stack. Phpmyadmin is a useful tool allowing a GUI for managing databases. Structured Query Language or SQL is a database language used to access, query, insert, update and delete from databases. MySQLi is an improved PHP extension allowing an API for manipulating a MySQL database via PHP. There are choices between a procedural or object-oriented approach to MySQLi.