PHP and MySQL.

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

+ Intro to PHP and SQL James Ohene-Djan. + What We’ll Cover Introduction to PHP Explain example code Introduction to Database Using PHP/MySQL for database.
Web Database Programming Connecting Database to Web.
PHP Week 11 INFM 603. Thinking About PHP Local vs. Web-server-based display HTML as an indirect display mechanism “View Source” for debugging –But not.
Everyday Italian Giada De Laurentiis Harry Potter J K. Rowling Learning XML Erik T. Ray CSCI 305 Introduction to Database.
Introduction to Database
PHP and MySQL PHP for the Web, page PHP and MySQL MySQL Resource PHP – MySQL Resource
Objectives Connect to MySQL from PHP
MySQL and PHP By Trevor Adams.
PHP Scripts HTML Forms Two-tier Software Architecture PHP Tools.
David Evans CS200: Computer Science University of Virginia Computer Science Class 29: Vocational Skills How to Build a.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
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,
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:
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)
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.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 19: Database Support.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Multifarious Project Team Members Alberto Dominguez Nirmit Gang Jimmy Garcia Javier Handal.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
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.
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.
MySQL PHP Web Technology. Logging in to Command Line Start -> Programs -> AppServ -> MySQL Command Line Client Enter Password, then you’ll be working.
MySQL Databases & PHP Integration Using PHP to write data to, and retrieve data from, a MySQL database.
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)
Database Access with PHP and MySQL CS356 Examples from Web Database Applications, by Hugh E. Williams & David Lane, O'Reilly, 2002.
PHP with MySQL 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.
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.
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.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Accessing mySQL relational database. MySQL database.  Today, we will attempt and open a connection to the MySQL server.  We need to specify the database.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CSC 405: Web Application Engineering II8.1 Web programming using PHP What have we learnt? What have we learnt? Underlying technologies of database supported.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: Vocational Skills (How to Build.
ITX2000 Remote hosts and web servers Prof. Xiaohong (Sharon) Gao Room: T125 Ext: Week 8 – Review Reference:
Tried my best to simplify it for you!
Remote hosts and web servers
PHP: Login FdSc Module 109 Server side scripting and Database design
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
Unix System Administration
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 ©
Advanced Internet Development Dynamic Web pages with php and MySQL
PHP.
Class 29: Vocational Skills How to Build a Dynamic Web Site
Erasmus Exchange in Ionian University
Software Engineering for Internet Applications
Introduction to Database
Web Programming– UFCFB Lecture
MySQL Web Application Connecting to a MySQL database
Database Access with PHP and MySQL
Conection
Presentation transcript:

PHP and MySQL

MySQL Open Source database server Runs on many platforms (Unix & Windows) Networked server – no fancy GUI like MS Access. You can find clients that provide a GUI. Great for small to medium-sized applications

MySQL Installation/Configuration You install the server, and provide a root password. Now you need a client to do anything! Create databases, view databases, etc. Windows MySQL server comes with a command-line client You need to learn all the commands, and type them in manually…

phpMyAdmin A MySQL client written in PHP Via the web you can manage: Manage Databases Manage MySQL users Submit queries (SQL) A great way to learn SQL!

Php – MySQL Support You need a version of PHP that includes the MySQL module Included by default on most php distributions. Documentation of all mysql functions/objects is available via php.net.

Opening a MySQL database $username=“fred”; $password=“fred”; $database=“eiw”; mysql_connect("localhost",$username,$password); @mysql_select_db($database) or die( "Unable to select database"); Assumes that the machine running the server is “localhost” You can easily use a MySQL server that is running on a remote machine.

Submitting a query to the server $query = "SELECT uid from users WHERE username = ‘fred’"; $res = mysql_query($query); // no result - no user so return false if (! $res) { … no result (error!) }

Accessing mysql_numrows($res) : number of rows in the result. mysql_result($res,$index,$field) Returns a single column value $index is the row $field is the field name (column name)

Example $res = mysql_query(“SELECT * FROM users”); if (! $res) { …handle error…} $numrows = mysql_numrows($res) for ($i=0;$i<$numrows;$i++) { $name = mysql_result($res,$i,”username”); $pass = mysql_result($res,$i,”password”); … Do something with $name and $password… }

Example – show data in the tables Function: list all tables in your database. Users can select one of tables, and show all contents in this table. second.php showtable.php http://www.cs.kent.edu/~nruan/second.php

second.php <html><head><title>MySQL Table Viewer</title></head><body> <?php // change the value of $dbuser and $dbpass to your username and password $dbhost = 'hercules.cs.kent.edu:3306'; $dbuser = 'nruan'; $dbpass = ‘*****************’; $dbname = $dbuser; $table = 'account'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if (!$conn) { die('Could not connect: ' . mysql_error()); } if (!mysql_select_db($dbname)) die("Can't select database");

second.php (cont.) $result = mysql_query("SHOW TABLES"); if (!$result) { die("Query to show fields from table failed"); } $num_row = mysql_num_rows($result); echo "<h1>Choose one table:<h1>"; echo "<form action=\"showtable.php\" method=\"POST\">"; echo "<select name=\"table\" size=\"1\" Font size=\"+2\">"; for($i=0; $i<$num_row; $i++) { $tablename=mysql_fetch_row($result); echo "<option value=\"{$tablename[0]}\" >{$tablename[0]}</option>"; echo "</select>"; echo "<div><input type=\"submit\" value=\"submit\"></div>"; echo "</form>"; mysql_free_result($result); mysql_close($conn); ?> </body></html>

showtable.php <html><head> <title>MySQL Table Viewer</title> </head> <body> <?php $dbhost = 'hercules.cs.kent.edu:3306'; $dbuser = 'nruan'; $dbpass = ‘**********’; $dbname = 'nruan'; $table = $_POST[“table”]; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if (!$conn) die('Could not connect: ' . mysql_error()); if (!mysql_select_db($dbname)) die("Can't select database"); $result = mysql_query("SELECT * FROM {$table}"); if (!$result) die("Query to show fields from table failed!" . mysql_error());

showtable.php (cont.) $fields_num = mysql_num_fields($result); echo "<h1>Table: {$table}</h1>"; echo "<table border='1'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td><b>{$field->name}</b></td>"; } echo "</tr>\n"; while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td>$cell</td>"; mysql_free_result($result); mysql_close($conn); ?> </body></html>

Functions Covered mysql_connect() mysql_select_db() include() mysql_query() mysql_num_rows() mysql_fetch_array() mysql_close()

Project Simple e-commerce site Users login in (with just a user name) View products (including search) Add products to shopping cart Remove products from shopping cart

Database Tables users – information about all the customers that use the system. products – information about all the products we sell. cartentries – shopping cart items (relates a user to a product)

Table: users uid: integer id number (autoincrement) firstname, lastname: strings. varchar(20) username: string – login name. varchar(20) email: string. varchar(30)

Table: products pid: integer id number (autoincrement) name: string – product name. varchar(30) price: floating point number.

Table: cartentries uid: integer user id number pid: integer product id number quantity: integer (# products).

Some Queries Get list of all products: “SELECT * FROM products” Get list of all the entries in joe’s (user 22) shopping cart: “SELECT * FROM cartentries WHERE uid=22” Check the actual code in the demo for more complex queries…

Sample Code: main.php main.php: the main program takes care of the session (session variable userid) Determines what the query is and takes appropriate action. Many actions defined in other PHP files that are included using require Generates the base HTML for the document (including a small “header”).

Database Code: db.php php functions that interact with the database. This file is always “required” by main.php. Creates connection to the mysql server. Functions login, product_list, show_cart, add_to_cart, remove_from_cart and some HTML generating functions.

add.php Called from main.php when user is adding an item to cart: require(“add.php”) Takes care of the logic for adding an item to the shopping cart for current user. Makes sure item exists.

login.php Called from main.php when user is trying to log in: require(“login.php”) Takes care of the logic for login process: Decides what to send back if valid/invalid login.

logout.php Called from main.php when user is trying to log out: require(“logout.php”) Takes care of the logic for log out: Terminates the session.

plist.php Called from main.php when user wants to see a list of products: require(“plist.php”) Just calls product_list function provided by db.php

remove.php Called from main.php when user is trying to remove an item from shopping cart: require(“remove.php”) Gets user id (from session) and product id (from HTTP query) Calls remove_from_cart Sends back resulting cart as HTML.

search.php Called from main.php when user is trying to search for products: require(“search.php”) If a search query is found in the HTTP query, processes the search. If no search query found, sends back a form that can be used to submit a search.

show.php Called from main.php when user is trying to see their shopping cart in: require(“show.php”) Just calls show_cart function found in db.php