PHP: Database Basic Selection FdSc Module 109

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

Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
PHP Hypertext Preprocessor Information Systems 337 Prof. Harry Plantinga.
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.
PHP and MySQL PHP for the Web, page PHP and MySQL MySQL Resource PHP – MySQL Resource
Objectives Connect to MySQL from PHP
Intermediate PHP & MySQL
PHP Scripts HTML Forms Two-tier Software Architecture PHP Tools.
PHP on a Fast Track a quick introduction to PHP programming by Jarek Francik.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
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,
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:
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
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.
_______________________________________________________________________________________________________________ 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.
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.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
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.
MySQL Database Connection
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
Retrieving data from MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
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.
Information Building and Retrieval Using MySQL Track 3 : Basic Course in Database.
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.
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)
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CSC 2720 Building Web Applications Accessing MySQL from PHP.
ITX2000 Remote hosts and web servers Prof. Xiaohong (Sharon) Gao Room: T125 Ext: Week 8 – Review Reference:
PHP and SQL Server: Connection IST 210: Organization of Data IST2101.
PHP AND SQL SERVER: CONNECTION IST 210: Organization of Data IST210 1.
Tried my best to simplify it for you!
Databases.
Introduction to Dynamic Web Programming
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
PHP: Inserting data FdSc Module 109 Server side scripting and
Web Design and Development
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: Output Formatting
ISC440: Web Programming 2 Server-side Scripting PHP 3
PHP: Security issues FdSc Module 109 Server side scripting and
Web Programming Language
PHP: Combo box FdSc Module 109 Server side scripting and
PHP: Database connection
Tutorial 6 PHP & MySQL Li Xu
Web Programming– UFCFB Lecture
MySQL Web Application Connecting to a MySQL database
PHP AND MYSQL.
Database Access with PHP and MySQL
Introduction to Web programming
Presentation transcript:

PHP: Database Basic Selection FdSc Module 109 Server side scripting and Database design 2011

PHP includes To avoid repeating code we can use an include to add an existing script to a new script Take the connection.php script and cut the $hostname, $username, $password and $databaseName and paste into a new file called basicSelection.php

basicSelection file <?php /* File: basicSelection.php By: Bob Date: 2012-03-04 This script illustrates the SQL SELECT command */ $hostname = “localhost"; $username = “MISNumber"; $password = "*******"; $databaseName = “MISNumber_library"; include("connection.php"); //adds the code from the connection.php script ?>

Remove the success messages We only need to know if the connection has failed We do not want to execute any further code it has failed We do this by setting a Boolean variable to indicate success in the connection.php script and test it in the basicSelection script

Modified connection script <?php error_reporting(0); $connectionSuccess = 0; // Initialise the Boolean variable $dbConnected = mysql_connect($hostname, $username, $password); $dbSelected = mysql_select_db ($databaseName,$dbConnected); if ($dbConnected) { if ($dbSelected) { $connectionSuccess = 1; //Set to 1 if connections OK } else { echo "DB Connected FAILED<br /><br />"; } echo "MySQL connection FAILED<br /><br />"; ?>

basicSelection.php <?php $hostname = “localhost"; $username = “MISNumber"; $password = "YourPassword"; $databaseName = “MISNumber_library"; include("connection.php"); echo $connectionSuccess; ?>

We now have a success variable When the script is viewed we should just see a 1 or an error message and a 0 We can now test the variable and proceed if it is 1 or exit if it is 0

Testing the variable <?php $hostname = “localhost"; $username = “MISNumber"; $password = "YourPassword"; $databaseName = “MISNumber_library"; include("connection.php"); if ($connectionSuccess == 1) { echo "Your code here"; } ?>

mysql_query The mysql_query command is used to run an SQL query The syntax is: $result = mysql_query("SQL statement"); Eg: $result = mysql_query("SELECT * FROM books"); $result contains a MySQL resource ID What use is that? To make use of it we need another function

mysql_fetch_array The mysql_fetch_array command is used to recover a row from the resource ID, which is the result of the query The syntax is: $row = mysql_fetch_array($result); To see what we have retrieved we can use a print_r($row) which will display the row contents on the screen

Displaying a row <?php // put your comment block and connection code here if ($connectionSuccess == 1) { $result = mysql_query("SELECT * FROM books"); $row = mysql_fetch_array($result); print_r($row); } ?>

The result Array ( [0] => 1 [bookID] => 1 [1] => ISBN [isbn] => ISBN [2] => Title [title] => Title [3] => Author [author] => Author [4] => Publisher [publisher] => Publisher [5] => Publisher's Web Site [publisherswebsite] => Publisher's Web Site [6] => Genre [genre] => Genre ) We can access the individual entries either by index number [1] or column name

Displaying selected columns if ($connectionSuccess == 1) { $result = mysql_query("SELECT * FROM books"); $row = mysql_fetch_array($result); echo $row[2]; echo "<br/>"; echo $row["author"]; }

The result Title Author But this only shows one row

Use of a while loop if ($connectionSuccess == 1) { $result = mysql_query("SELECT * FROM books"); while ($row = mysql_fetch_array($result)) { echo $row[2]; echo "<br/>"; echo $row["author"]; echo "<br/><br/>"; } mysql_free_results($result); //free up server memory

The result Title Author "The Definitive Guide to MySQL 5, Third Edition (Definitive Guide)“ Michael Kofler PHP Cookbook (Cookbooks (O'Reilly)) David Sklar Harry Potter and the Philosopher's Stone (Book 1) J.K. Rowling And so it goes

Escaping PHP for HTML if ($connectionSuccess == 1) { $result = mysql_query("SELECT * FROM books"); while ($row = mysql_fetch_array($result)) { echo $row[2]; echo "<br/>"; echo $row["author"]; ?> <br/><br/> <?php //This restores PHP after escaping it for the HTML } mysql_free_results($result); //free up server memory

Exercises Try: retrieving different columns formatting the results using HTML and CSS