Web Programming– UFCFB Lecture

Slides:



Advertisements
Similar presentations
PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Advertisements

Objectives Connect to MySQL from PHP
MySQL and PHP By Trevor Adams.
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.
Website Development Working with MySQL. What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql.
ADVM420- Class #4 Web Design with PHP and MySQL Adding and Listing from a MySQL Database.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
Programming with php By: Seth Larson. A little bit about PHP  PHP stands for PHP:  Hypertext Preprocessor  PHP is a widely-used general-purpose server-side.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
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.
Slide 8-1 CHAPTER 8 Using Databases with PHP Scripts: Using MySQL Database with PHP.
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:
PHP Data Object (PDO) Khaled Al-Sham’aa. What is PDO? PDO is a PHP extension to formalise PHP's database connections by creating a uniform interface.
PHP – MySQL Extensions. Table used in most examples CREATE TABLE product ( rowID INT NOT NULL AUTO_INCREMENT, productid VARCHAR(8) NOT NULL, name VARCHAR(25)
Lecture 9 – MYSQL and PHP (Part1) SFDV3011 – Advanced Web Development 1.
INTERNET APPLICATION DEVELOPMENT PRACTICAL ON CONNECTING TO MYSQL.
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.
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.
MySQL and PHP 3 March 2006 Adina Crainiceanu IT420: Database Management and Organization.
PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.
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.
Lecture 10 – MYSQL and PHP (Part 2)
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
MySQL Database Connection
Web-Based Database Programming with PHP. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn PHP Basics PHP functions –To.
Pengantar Teknologi Internet W14: Server Scripting & Database.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
PHP Database connectivity Connecting with RDBMS and editing, adding, and deleting databases therein are all done through PHP functions.
PHP Database Processing CIS 1715 Web Technologies.
PHP Workshop ‹#› PHP Data Object (PDO). PHP Workshop ‹#› What is PDO? PDO is a PHP extension to formalise PHP's database connections by creating a uniform.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
PHP Workshop ‹#› أطلق إبداعك 2 أطلق إبداعك 2 مدرس معتمد من مركز زووم PHP Data Object (PDO)
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.
Accessing mySQL relational database. MySQL database.  Today, we will attempt and open a connection to the MySQL server.  We need to specify the database.
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CHAPTER 10 PHP MySQL Database
PHP with MYSQL Please use speaker notes for additional information!
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
ITX2000 Remote hosts and web servers Prof. Xiaohong (Sharon) Gao Room: T125 Ext: Week 8 – Review Reference:
1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in PHP consists of a number of functions.
CS242 SQL. What is SQL? SQL:  stands for Structured Query Language  allows you to access a database  is an ANSI standard computer language  can execute.
Web Systems & Technologies
Tried my best to simplify it for you!
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Unix System Administration
Web Design and Development
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 ©
Introduction to Web programming
PHP and MySQL.
ISC440: Web Programming 2 Server-side Scripting PHP 3
Erasmus Exchange in Ionian University
MySQL Web Application Connecting to a MySQL database
Software Engineering for Internet Applications
PHP: Database Basic Selection FdSc Module 109
PHP: Database connection
MySQL Web Application Connecting to a MySQL database
PHP AND MYSQL.
Database Access with PHP and MySQL
Introduction to Web programming
Presentation transcript:

Web Programming– UFCFB3-30-1 Lecture 24-25-26 Instructor : Mazhar H Malik Email : mazhar@gcet.edu.om Global College of Engineering and Technology

MYSQL and PHP

Performing MySQL queries from PHP 1. Connect to the MySQL database 2. Select the database to use 3. Perform the desired SQL queries 4. Retrieve data returned from the queries 5. Close the connection to the database

1. Connecting to the database Use mysql_connect: mysql_connect([$hostname [,$username [,$password [,$dbname [,$port [,$socket]]]]]]); In the lab: mysql_connect($hostname, $username, $password); Where: $hostname = "localhost"; $username = your MySQL username (by defaut "root") $password = your MySQL password (by default "")

1. Connecting to the database Example, with error handling: $link = mysql_connect("localhost", "root", "''); if (!$link) die('Could not connect: ' . mysql_error()); else echo "Connection successful!";

2. Select the database to use Use mysql_select_db: mysql_select_db($dbname, $link); Example: mysql_select_db('visitdb', $link);

2. Select the database to use Example, with error checking: $db_selected = mysql_select_db('visitdb', $link); if (!$db_selected) die('Can\'t use visitdb: ' . mysql_error());

3. Performing a basic query Use: mysql_query: mysql_query($query [, $link]); Example: $query = "SELECT * FROM visitors"; $result = mysql_query($query, $link);

3. Performing a basic query Example, with error checking: $query = "SELECT * FROM visitors"; $result = mysql_query($query, $link); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n";  die($message); }

4. Retrieving data returned by the query Output of the mysql_query function doesn’t hold the data Used as a reference to fetch the table rows returned Fetching done using one of: mysql_fetch_array mysql_fetch_assoc mysql_fetch_row

Using mysql_fetch_array Version 1 – using numerical indexing: while ($row = mysql_fetch_array($result, MYSQL_NUM)) { echo "$row[0]<br>"; } This displays the value of the first field (index=0) of all the records returned by the query

Using mysql_fetch_array Version 2 – using key-value pairs: while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "row[name]<br>"; } This displays the value associated to the key ‘name’ of all the records returned by the query

Using the other fetch functions mysql_fetch_assoc($result)  mysql_fetch_array($result, MYSQL_ASSOC) mysql_fetch_row($result)  mysql_fetch_array($result, MYSQL_NUM)

5. Closing the db connection Use mysql_close: mysql_close($link); Where $link represents an opened connection

$num = mysql_num_rows($result); Count rows Use mysql_num_rows: $num = mysql_num_rows($result); Where: $result is the result of a query.

Count columns Use mysql_num_fields: $num = mysql_num_fields($result); Example: $result = mysql_query("SELECT name ,email FROM visitors"); echo mysql_num_fields($result); // return 2

Go to (http://localhost/phpmyadmin )Where it says Create database, enter a name and click on the Create button

Create a table to hold the data. Give it a name Create a table to hold the data. Give it a name. Select a number of columns and click Go.

Lecture10a.sql Copy this code and paste it in SQL. create table if not exists visitors( visitorID int unsigned not null auto_increment primary key, name char(50) not null, email char(50), Affiliation char(200)); create table if not exists organisations( organisationID int unsigned not null auto_increment primary key, address char(200), notes text); create table if not exists visits( visitID int unsigned not null auto_increment primary key, visitorID int unsigned not null, organisationID int unsigned not null, date date); insert into visitors (name, email) values ('John Doe', 'j.d@napier.ac.uk'), ('Jim Doe', 'ji.d@napier.ac.uk'), ('Jon Doe', 'jo.d@napier.ac.uk'); insert into organisations (name, address, notes) values ('ACME', 'New York', NULL), ('abcde.com', 'Bathgate', 'No comment!'); insert into visits values (NULL, 1, 2, '2000-12-20'), (NULL, 2, 2, '2004-04-21'), (NULL, 1, 2, '2006-05-06'); Lecture10a.sql Copy this code and paste it in SQL.

Example lecture10b.php Lecture10b.php <?php /*Step 1: Open a connection to the database (server_name=localhost, default username=root and default password=root */ $link= mysql_connect("localhost", "root", ""); //Step 2: Select a database mysql_select_db("visitdb", $link); //Step 3a: Create the query string $query="SELECT * FROM visitors"; //Step 3b: Execute the query $result = mysql_query($query, $link); //Step 4: Loop through the result set, doing something useful with it while ($row = mysql_fetch_array($result, MYSQL_NUM)) { echo "<p>"; foreach ($row as $x) echo $x. " "; echo "</p>"; } //Step 5: Close database connection mysql_close($link); ?> VisitorID Name Email Affiliation 1 John Doe j.d@napier.ac.uk 2 Jim Doe ji.d@napier.ac.uk 3 jo.d@napier.ac.uk

How to run lecture10b.php file? Save the file lecture10b.php in the folder c:/xampp/htdocs Go to Internet Explorer In the URL address type http://localhost/lecture10b.php

Run the lecture10b.php file to get the results below Visitors VisitorID Name Email Affiliation 1 John Doe j.d@napier.ac.uk 2 Jim Doe ji.d@napier.ac.uk 3 jo.d@napier.ac.uk

PDO PDO (PHP Data Objects) is a PHP extension to formalize PHP's database connections by creating a uniform interface. This allows developers to create code which is portable across many databases and platforms. PDO supports the following databases: Microsoft SQL Server / Sybase , Firebird / Interbase, DB2 / INFORMIX (IBM), MySQL, OCI (Oracle Call Interface), ODBC, PostgreSQL, SQLite There are three simple rules to use PDO: Use exec() when there is no result set (will tell you how many rows affected) Use query() when there are rows to return, and the query will be run once, with no variable parameters Use a prepared statement when the query will be run with variable parameters PDO_MYSQL is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to MySQL 3.x, 4.x and 5.x databases.

Lecture10c.php 1. Use exec() when there is no result set (will tell you how many rows affected) <?php Try { //steps 1 and 2 $dbh = new PDO("mysql:host=localhost;dbname=visitdb", "root", ""); //step 3 $query = "INSERT INTO visitors(Name,Email,Affiliation) VALUES ('michael', 'michael@yahoo.com', 'Otago University')"; //step 4 $affected = $dbh->exec($query); echo "Total rows affected: $affected"; //step 5 $dbh=null;//close connection } catch(PDOException $e) { echo $e->getMessage(); ?> VisitorID Name Email Affiliation 1 John Doe j.d@napier.ac.uk 2 Jim Doe ji.d@napier.ac.uk 3 jo.d@napier.ac.uk 4 michael michael@yahoo.com

How to run lecture10c.php file? Save the file lecture10c.php in the folder c:/xampp/htdocs Go to Internet Explorer In the URL address type http://localhost/lecture10c.php

Run the lecture10c.php file to get the results below

$dbh = new PDO("mysql:host=localhost;dbname=visitdb", "root", ""); 2. Use query() when there are rows to return, and the query will be run once, with no variable parameters Lecture10d.php <?php Try { $dbh = new PDO("mysql:host=localhost;dbname=visitdb", "root", ""); $query = "SELECT Name, Email FROM visitors"; foreach ($dbh->query($query) as $row) $Name = $row['Name']; $Email = $row['Email']; echo "Name: $Name Email: $Email<br>"; } $dbh=null;//close connection catch(PDOException $e) echo $e->getMessage(); ?> VisitorID Name Email Affiliation 1 John Doe j.d@napier.ac.uk 2 Jim Doe ji.d@napier.ac.uk 3 jo.d@napier.ac.uk 4 michael michael@yahoo.com

Run the lecture10d.php file to get the results below

$dbh = new PDO("mysql:host=localhost;dbname=visitdb", "root", ""); 3. Use a prepared statement when the query will be run with variable parameters Lecture10e.php <?php try{ $dbh = new PDO("mysql:host=localhost;dbname=visitdb", "root", ""); $query = "SELECT * FROM visitors"; $stmt = $dbh->prepare($query); //$stmt->bindParam(":Name", ":John Doe"); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); print_r($result); $dbh=null; //close connection } catch(PDOException $e) { echo $e->getMessage(); ?> VisitorID Name Email Affiliation 1 John Doe j.d@napier.ac.uk 2 Jim Doe ji.d@napier.ac.uk 3 jo.d@napier.ac.uk 4 michael michael@yahoo.com

Run the lecture10e.php file to get the results below