© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,

Slides:



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

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.
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
Intermediate PHP & MySQL
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.
PhpMyAdmin What is PhpMyAdmin?  PhpMyAdmin is one of the most popular applications for MySQL databases management. It is a free tool written in PHP.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
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.
Deleting and Updating Records in MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
LIS651 lecture 7 PHP mySQL Thomas Krichel
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:
Create an online booking system (login/registration)
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
PHP MySQL Introduction
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
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.
_______________________________________________________________________________________________________________ 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.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Lec_6 Manipulating MySQL Databases with PHP PHP Programming with MySQL.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
MySQL Databases & PHP Integration Using PHP to write data to, and retrieve data from, a MySQL database.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Lecture 10 – MYSQL and PHP (Part 2)
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.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
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.
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.
2010/11 : [1]PHP with MySQLBuilding Web Applications using MySQL and PHP (W1) PHP with MySQL.
PHP Database Processing CIS 1715 Web Technologies.
PHP and Mysql Database. PHP and Database Mysql – popular open-source database management system PHP usually works with Mysql for web-based database applications.
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)
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.
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.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
CHAPTER 10 PHP MySQL Database
>> PHP: MySQL & CRUD. R ecall Database Tables Records is composed of Operations (CRUD) Create Retrieve Update Delete DBMS Access Control MySQL phpMyAdmin.
CSC 2720 Building Web Applications Accessing MySQL from PHP.
MySQL MySQL and PHP – interacting with a database.
Working With Database Library And Helpers. Connecting to your Database First you need to set parameters in you database.php file residing in config folder.
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.
Tried my best to simplify it for you!
PHP: MySQL Lecture 14 Kanida Sinmai
Web Systems & Technologies
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Introduction to Web programming
PhpMyaAmin & MySQL PHP: Hypertext Preprocessor.
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: Database Basic Selection FdSc Module 109
Tutorial 6 PHP & MySQL Li Xu
Web Programming– UFCFB Lecture
Introduction to Web programming
Presentation transcript:

© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING, PART-2 Chapter 19of Text Book DATABSE Connection to MySQL / PHP Internet & World Wide Web How to Program, 5/e

© Yanbu University College DATABASE CONNECTIVITY PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform) PHP Connect to the MySQL Server Use the PHP mysqli_connect() function to open a new connection to the MySQL server. Open a Connection to the MySQL Server Before we can access data in a database, we must open a connection to the MySQL server. In PHP, this is done with the mysqli_connect() function. Syntax mysqli_connect(host, username, password, dbname); STEPS 1.CREATE A USER in PHPMyAdmin 2.CREATE A DATABASE 3.CONNECT TO DATABASE 4.DATA RETREIVAL/ MANIPULATION

© Yanbu University College

PHP Create Database and Tables A database holds one or more tables. Create a Database The CREATE DATABASE statement is used to create a database table in MySQL. We must add the CREATE DATABASE statement to the mysqli_query() function to execute the command. The following example creates a database named “MIS352": <?php $con=mysqli_connect(“localhost",“haseena",“husna"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); } // Create database $sql="CREATE DATABASE MIS352"; if (mysqli_query($con,$sql)) { echo "Database MIS352 created successfully"; } else { echo "Error creating database: ". mysqli_error(); } ?>

© Yanbu University College Create a Table The CREATE TABLE statement is used to create a table in MySQL. We must add the CREATE TABLE statement to the mysqli_query() function to execute the command. The following example creates a table named “MArks", with three columns. The column names will be “STD_ID", “STD_NAME" and “STD_MARKS": <?php $con=mysqli_connect("localhost","PHPClass","hello","MIS352"); // Check connection if(mysqli_connect_errno()) { echo "Failed to connect to MySQL:". mysqli_connect_error(); } // Create table $sql="CREATE TABLE Marks(STD_ID CHAR(30),STD_NAME CHAR(30),STD_MARKS INTO(20))"; // Execute query if(mysqli_query($con,$sql)) {echo "Table Marks created successfully";} else { echo "Error in creating table: ". mysqli_error(); } ?> Note: When you create a database field of type CHAR, you must specify the maximum length of the field, e.g. CHAR(50). The data type specifies what type of data the column can hold.

© Yanbu University College PHP MySQL Insert Into Insert Data Into a Database Table The INSERT INTO statement is used to add new records to a database table. Syntax It is possible to write the INSERT INTO statement in two forms. The first form doesn't specify the column names where the data will be inserted, only their values: INSERT INTO table_name VALUES (value1, value2, value3,...) The second form specifies both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

© Yanbu University College PHP MySQL Insert Into Example:In the previous slide we created a table named “Marks", with three columns; “STD_ID", “STD_Name" and “STD_Marks". We will use the same table in this example. The following example adds two new records to the “Marks" table: <?php // connect with the database and user in Php Myadmin $n=mysqli_connect("localhost","PHPClass","hello","MIS352"); if (mysqli_connect_errno()) { echo(" Error In connection"); } else { echo("database connected"); } $stuid=($_POST['sid']); $stuname=($_POST['sn']); $stumarks=intval($_POST['sa']); $sql="insert into Marks(STD_ID,STD_NAME,STD_MARKS) values ('$stuid','$stuname',$stumarks)"; if(mysqli_query($n,$sql)) {echo " Record added successfully";} else {echo"error in insertion";} mysqli_close($n); ?>

© Yanbu University College Insert Data From a Form Into a Database HTML FILE Insert your Detail in the DATABSE ID NAME MARKS OUT PUT Now we will create an HTML form that can be used to add new records to the “MIS352" table. Here is the HTML form:

© Yanbu University College Insert Data From a Form Into a Database Inserting record When a user clicks the submit button in the HTML form in the example above, the form data is sent to "insert.php". The "insert.php" file connects to a database, and retrieves the values from the form with the PHP $_POST variables. Then, the mysqli_query() function executes the INSERT INTO statement, and a new record will be added to the “Marks" table. Here is the "insert.php" page:

© Yanbu University College Insert Data From a Form Into a Database(contd) Insert.php <?php // connect with the database and user in Php Myadmin $n=mysqli_connect("localhost","PHPClass","hello","MIS352"); if (mysqli_connect_errno()) { echo(" Error In connection"); } else { echo("database connected"); } $stuid=($_POST['sid']); $stuname=($_POST['sn']); $stumarks=intval($_POST['sa']); $sql="insert into Marks(STD_ID,STD_NAME,STD_MARKS) values ('$stuid','$stuname',$stumarks)"; if(mysqli_query($n,$sql)) {echo " Record added successfully";} else {echo"error in insertion";} mysqli_close($n); ?> OUTPUT:

© Yanbu University College PHP MySQL Select Select Data From a Database Table The SELECT statement is used to select data from a database. Syntax SELECT column_name(s) FROM table_name To get PHP to execute the statement above we must use the mysqli_query() function. This function is used to send a query or command to a MySQL connection. The example below stores the data returned by the mysql_query() function in the $result variable. Next, we use the mysqli_fetch_array() function to return the first row from the recordset as an array. Each call to mysqli_fetch_array() returns the next row in the recordset. The while loop loops through all the records in the recordset. To print the value of each row, we use the PHP $row variable ($row['FirstName'] and $row['LastName']).

© Yanbu University College <?php echo " The contents of Marks Table: "; $con=mysqli_connect("localhost","PHPClass","hello","MIS352"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM Marks"); echo " STD_ID STD_NAME STD_MARKS "; while($row = mysqli_fetch_array($result)) { echo " "; echo " ". $row['STD_ID']. " "; echo " ". $row['STD_NAME']. " "; echo " ". $row['STD_MARKS']. " "; echo " "; } echo " "; mysqli_close($con); ?> DATABASE CONNECTIVITY Complete example WITH mysqli_connect()

© Yanbu University College ID NAME <?php //connection to the database $con=mysqli_connect("localhost",”haseena",”husna","mis352"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); } //execute the SQL query and return records $result = mysqli_query($con,"SELECT * FROM student"); //fetch tha data from the database while($row = mysqli_fetch_array($result)) { //display the results echo $row['ID']. " ". $row['NAME']. " ". $row[' ']; echo " "; } //close the connection mysqli_close($con); ?> DATABASE CONNECTIVITY Complete example WITH mysqli_connect()

© Yanbu University College Output

© Yanbu University College Die() Function Definition and Usage The die() function prints a message and exits the current script. This function is an alias of the exit() function. E.g. //connection to the database $dbhandle = mysql_connect(‘hostname’, ‘username’, ‘password’) or die("Unable to connect to MySQL");

© Yanbu University College DATABASE CONNECTIVITY WITH mysql_connect() <?php //connection to the database $dbhandle = mysql_connect(‘hostname’, ‘username’, ‘password’) or die("Unable to connect to MySQL"); echo "Connected to MySQL "; //select a database to work with $selected = mysql_select_db("examples",$dbhandle) or die("Could not select examples"); //execute the SQL query and return records $result = mysql_query("SELECT id, model,year FROM cars"); //fetch tha data from the database while ($row = mysql_fetch_array($result)) { echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ". //display the results $row{'year'}." "; } //close the connection mysql_close($dbhandle); ?>

© Yanbu University College PHP MySQL The Where Clause <?php $con=mysqli_connect("localhost",”PHPClass",”hello","mis352"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM student WHERE NAME='hia' "); while($row = mysqli_fetch_array($result)) { echo $row['ID']. " ". $row['NAME']. " ". $row[' ']; echo " "; } ?>

© Yanbu University College PHP MySQL Order By Keyword The ORDER BY Keyword The ORDER BY keyword is used to sort the data in a recordset in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword <?php $con=mysqli_connect("localhost",”PHPClass",”Hello","mis352"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM student ORDER BY ID "); while($row = mysqli_fetch_array($result)) { echo $row['ID']. " ". $row['NAME']. " ". $row[' ']; echo " "; } ?>

© Yanbu University College PHP MySQL Update The UPDATE statement is used to modify data in a table. <?php $con=mysqli_connect("localhost",”PHPClass",”Hello","mis352"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); } $b ="UPDATE student SET ID=36 WHERE NAME=‘Hasi' AND if (mysqli_query($con,$b)) { echo("RECORD UPDATED SUCCESFUL "); } ?>

© Yanbu University College PHP MySQL Delete The DELETE FROM statement is used to delete records from a database table. <?php $con=mysqli_connect("localhost","PHPClass","hello","MIS352"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); } $a="DELETE FROM Marks WHERE STD_NAME='Malak'"; if (mysqli_query($con,$a)) { echo "1 record Deleted"; } else { echo "Error in Deletion the record: ". mysqli_error(); } mysqli_close($con); ?> OUTPUT: