Tried my best to simplify it for you!

Slides:



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

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap.
Objectives Connect to MySQL from PHP
Intermediate PHP & MySQL
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
MySql In Action Step by step method to create your own database.
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.
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
Deleting and Updating Records in MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
© 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.
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.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
INTERNET APPLICATION DEVELOPMENT PRACTICAL ON CONNECTING TO MYSQL.
 Mysql – popular open-source database management system  PHP usually works with Mysql for web- based database applications  LAMP applications—Web-based.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
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.
PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null.
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.
INTERNET APPLICATION DEVELOPMENT Practical on Sessions.
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
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.
Retrieving data from MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
PHP Database Processing CIS 1715 Web Technologies.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
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.
Module Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
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.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
CHAPTER 10 PHP MySQL Database
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
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.
 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.
PHP & MY SQL Instructor: Monireh H. Sayadnavard 1.
Web Systems & Technologies
Web Systems & Technologies
Databases.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Unix System Administration
Session 4 PHP & MySQL.
Introduction to Web programming
Unit 2 MY SQL DATABASE PROGRAMMING
PHP + MySQL Commands Refresher.
MySQL tutorial.
PHP and MySQL.
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter 8 Working with Databases and MySQL
MySQL Web Application Connecting to a MySQL database
Web Programming Language
PHP: Database Basic Selection FdSc Module 109
Tutorial 6 PHP & MySQL Li Xu
Web Programming– UFCFB Lecture
Introduction to Web programming
Presentation transcript:

Tried my best to simplify it for you! PHP & MySQL

PHP Functions There are two parts which should be clear to you: Creating a PHP Function Calling a PHP Function

Sample </body> <html> <body> <head> <title>Writing PHP Function</title> </head> <body> <?php /* Defining a PHP Function */ function writeMessage() { echo “Exam is knocking at the door,<BR> Are you ready?"; } /* Calling a PHP Function */ writeMessage(); ?> </body> </html> Exam is knocking at the door, Are you ready?

PHP functions with parameters <html> <head> <title>Writing PHP Function with Parameters</title> </head> <body> <?php function addFunction($num1, $num2) { $sum = $num1 + $num2; echo "Sum of the two numbers is : $sum"; } addFunction(10, 20); ?> </body> </html> Would the answer still remain the same if this stmt was outside the function? Sum of the two numbers is : 30

Passing Arguments by Reference A reference to the variable is manipulated by the function rather than a copy of the variable's value. Any changes made to an argument in these cases will change the value of the original variable. Add an ampersand to the variable name in either the function call or the function definition.

Pass by Reference <?php function addFive ($num) { $num += 5; } function addSix (&$num) $num += 6; $x = 10; addFive ( &$x ); echo "Original Value is $x <br />"; addSix ( $x ); echo "Original Value is $x <br />"; ?> Original Value is 15 Original Value is 21

Functions returning a value A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code.

Example <?php function addFunction($num1, $num2) { $sum = $num1 + $num2; return $sum; } $return_value = addFunction(10, 20); echo "Returned value from the function : $return_value”; ?> Returned value from the function : 30

Highlights of the lecture Creation of table Insertion of records Retrieval of records

What came first the egg or ….? So what comes first the Table or the Database?

Creation of Database is a must This is the very first step involved in your php-mysql operations.

Start up Server2Go

Conversation between Gary and Sumedha: Hey good morning Sumedha! Sumedha: Watzz so good about the morning Gary ? The good thing about this morning is dat a gal called Sumedha is going to help me understand phpMyAdmin So what is phpMyAdmin? You can’t ask me the same question back again. I know that, so I was saying….. You were saying….. 1. phpMyAdmin is a tool . 2. It is open source and free 3. Allows you to view all the MySQL DB, tables and entries. Can we execute sql queries too? Ofcourse you can do so via the web browser.

Click on phpMyAdmin

On Successful Login… CollegeDB

On clicking Create, see what happens….

Sumedha decides to test the knowledge of Gary Some basic questions Sumedha decides to test the knowledge of Gary Sumedha: What is MySQL? Gary: It is a database What does this database contain? The data in MySQL is stored in database objects called tables. What is a table? A table is a collection of related data entries and it consists of columns and rows. So you think your smart huh! Whats to think in that Sumedha? Ok Smarty, can you give me an example of a database? A company may have a database with the following tables: "Employees", "Products", "Customers" and "Orders". Howz that for you sweety? Ok ok, that answer has finally convinced me that you know your basics right.

Create a Connection to a MySQL Database First creation of database along with a connection to it is a must. In PHP, this is done with the mysql_connect() function. Syntax mysql_connect(servername,username,password);

mysql_connect(servername,username,password);

What is $con? <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con)   {   die('Could not connect: ' . mysql_error());   } // some code ?>

OR <?php $con = mysql_connect("localhost","root",""); if($con) else { echo "Connection Successful"; } else { echo"Connection Error:".die(mysql_error()); } ?>

Closing the connection Although, the connection will be closed automatically when the script ends. But to close the connection before, use the mysql_close() function: mysql_close($con);

Can we create a Database in php? The CREATE DATABASE statement is used to create a database in MySQL. CREATE DATABASE database_name

Code: <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_query("CREATE DATABASE emp_db",$con)) { echo "Database was created"; } else echo "Error creating database: " . mysql_error(); mysql_close($con); ?>

Letz check and see if the Database was created?

The die() function The die() function prints a message and exits the current script. This function is an alias of the exit() function. Syntax: die(message);

Manually destroying a database

Adding tables to a database Syntax of MySQL CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... )

Php example code Create a database “emp_db” Create a table called “tbl_employee” in the “emp_db” database. Fields of tbl_employee are as follows: emp_id emp_firstname emp_lastname emp_post

Code Part1 + Part2 Step 1: Connect to the mysql via php <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } //code continues…

Step2: Creation of Database if (mysql_query( "CREATE DATABASE emp_db1",$con)) { echo "Database created"; } else echo "Error creating database: " . mysql_error(); } //code continues…

Step 3: Creation of Table mysql_select_db("emp_db1", $con); $my_query = "CREATE TABLE tbl_employee ( emp_Id int, emp_FirstName varchar(15), emp_LastName varchar(15), emp_post varchar(15) )"; // Execute query mysql_query($my_query,$con);

On Clicking Data Dictionary

Auto Increment of Id $my_query = "CREATE TABLE tbl_employee ( )"; mysql_select_db("emp_db1", $con); $my_query = "CREATE TABLE tbl_employee ( emp_Id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(emp_id), emp_FirstName varchar(15), emp_LastName varchar(15), emp_post varchar(15) )";

Insert Data into a Database Table The INSERT INTO statement is used to insert new records in a table. 2 ways of doing it: 1st form: Directly insert without specifying column names. 2nd form: Specify column names and values too.

Direct Insertion without column names INSERT INTO table_name VALUES ( value1, value2, value3,...) To get PHP to execute the statements above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection. Insertion with THE use of column names INSERT INTO table_name (column1, column2, column3,...) VALUES ( value1, value2, value3,...)

Inserting Data mysql_select_db("emp_db1", $con); $sql = "INSERT INTO tbl_employee (emp_FirstName, emp_LastName, emp_post) VALUES ('Frazer', 'Mascarenhas','Principal')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";

Inserting data via html form+post You need: An HTML form to read the data And a php form to process the data

27th September 2012 Recall: Set up a database called College_DB Created a table called tbl_employee Having structure????? Inserted 2 records via html form.

Displaying Data via Selection

Select SELECT column_name(s) FROM table_name mysql_query("SELECT * FROM tbl_employee");

mysql_fetch_array() function Function used to return the first row from the recordset as an array. Each call to mysql_fetch_array() returns the next row in the recordset. A recordset is a data structure that consists of a group of database records, and can either come from a base table or as the result of a query to the table.

mysql_select_db("emp_db1", $con); <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("emp_db1", $con); $result = mysql_query("SELECT * FROM tbl_employee"); while( $row = mysql_fetch_array($result) ) echo $row['emp_FirstName'] . " " . $row['emp_LastName']; echo "<br />"; mysql_close($con); ?>

Role of -- The while loop 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[‘emp_FirstName'] and $row[‘emp_LastName']).

Display the Result in an HTML Table

$result = mysql_query("SELECT * FROM tbl_employee"); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['emp_FirstName'] . "</td>"; echo "<td>" . $row['emp_LastName'] . "</td>"; echo "</tr>"; } echo "</table>";

$result = mysql_query("SELECT * FROM tbl_employee"); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['emp_FirstName'] . "</td>"; echo "<td>" . $row['emp_LastName'] . "</td>"; echo "</tr>"; } echo "</table>";

Where Clause SELECT column_name(s) FROM table_name WHERE column_name operator value Use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

$result = mysql_query("SELECT * FROM tbl_employee <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("emp_db1", $con); $result = mysql_query("SELECT * FROM tbl_employee WHERE emp_FirstName='Jyoti'"); while($row = mysql_fetch_array($result)) echo $row['FirstName'] . " " . $row['LastName']. " ". $row['post']; echo "<br />"; ?>

The ORDER BY Keyword The ORDER BY keyword is used to sort the data in a recordset. The ORDER BY keyword sort the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword.

SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC The following example selects all the data stored in the “tbl_employee" table, and sorts the result by the “emp_LastName" column

ORDER BY emp_LastName

Order by Two Columns SELECT column_name(s) FROM table_name ORDER BY column1, column2 possible to order by more than one column. the second column is only used if the values in the first column are equal

Order by emp_FirstName, emp_LastName