Database Connectivity using PHP Some Points to Remember:

Similar documents
PHP Development - Introduction

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

By the end of this section of the practical, the students should be able to:

Web Programming. Dr Walid M. Aly. Lecture 10 PHP. lec10. Web Programming CS433/CS614 22:32. Dr Walid M. Aly

Development Technologies. Agenda: phpmyadmin 2/20/2016. phpmyadmin MySQLi. Before you can put your data into a table, that table should exist.

Using PHP with MYSQL

Networks and Web for Health Informatics (HINF 6220) Tutorial 13 : PHP 29 Oct 2015

PHP Arrays. Lecture 20. Robb T. Koether. Hampden-Sydney College. Wed, Feb 28, 2018

COM1004 Web and Internet Technology

Create Basic Databases and Integrate with a Website Lesson 3

Lab 7 Introduction to MySQL

PHP Introduction. Some info on MySQL which we will cover in the next workshop...

COMP519: Web Programming Autumn 2015

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Lecture 13: MySQL and PHP. Monday, March 26, 2018

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Web Application Development (WAD) V th Sem BBAITM (Unit 4) By: Binit Patel

CHAPTER 10. Connecting to Databases within PHP

MySQL: Access Via PHP

Chapter. Accessing MySQL Databases Using PHP

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

Chapter 7 PHP Files & MySQL Databases

Advanced Web Programming Practice Exam II

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

Form Processing in PHP

AN INTRODUCTION TO WEB PROGRAMMING. Dr. Hossein Hakimzadeh Department of Computer and Information Sciences Indiana University South Bend, IN

BSc (Hons) Web Technologies. Examinations for 2017 / Semester 1

Use of PHP for DB Connection. Middle and Information Tier. Middle and Information Tier

More loops. Control structures / flow control. while loops. Loops / Iteration / doing things over and over and over and over...

Mount Saint Mary College, Newburgh, NY Internet Programming III - CIT310

Chapters 10 & 11 PHP AND MYSQL

ITS331 IT Laboratory I: (Laboratory #11) Session Handling

HTML Forms & PHP. Database Systems CSCI Dr. Tom Hicks Computer Science Department

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

Overview of MySQL Structure and Syntax [2]

PHP Tutorial 6(a) Using PHP with MySQL

Web Systems Nov. 2, 2017

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus

2017 Politecnico di Torino 1

Daniel Pittman October 17, 2011

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

2017 Politecnico di Torino 1

B. V. Patel Institute of BMC & IT 2014

Chapter 6 Part2: Manipulating MySQL Databases with PHP

Lecture 5. Monday, September 15, 2014

CMPS 401 Survey of Programming Languages

APLIKACJE INTERNETOWE 8 PHP WYKORZYSTANIE BAZY DANYCH MYSQL

CPSC 421 Database Management Systems. Lecture 10: Embedded SQL

Executing Simple Queries

A QUICK GUIDE TO PROGRAMMING FOR THE WEB. ssh (then type your UBIT password when prompted)

CMSC 330: Organization of Programming Languages. Markup & Query Languages

CMSC 330: Organization of Programming Languages

LAMP Apps. Overview. Learning Outcomes: At the completion of the lab you should be able to:

UNIT V ESTABLISHING A DATABASE CONNECTION AND WORKING WITH DATABASE

Use of PHP for DB Connection. Middle and Information Tier

MULTIMEDIA AND WEB TECHNOLOGY

PHP Reference. To access MySQL manually, run the following command on the machine, called Sources, where MySQL and PhP have been installed:

PHP: Hypertext Preprocessor. A tutorial Introduction

Multimedia im Netz Online Multimedia Winter semester 2015/16

2018 Computing Science. Advanced Higher. Finalised Marking Instructions

Module 3 MySQL Database. Database Management System

CPET 499/ITC 250 Web Systems

If Only. More SQL and PHP

PHP 5 if...else...elseif Statements

IS 2150 / TEL 2810 Introduction to Security

PHP INTERVIEW QUESTION-ANSWERS

Jquery Ajax Json Php Mysql Data Entry Example

CSC 3300 Homework 3 Security & Languages

Databases and PHP. Accessing databases from PHP

home.php 1/1 lectures/6/src/ include.php 1/1 lectures/6/src/

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 3

Lecture 6 Session Control and User Authentication. INLS 760 Web Databases Spring 2013 Rob Capra

Locate your Advanced Tools and Applications

P - 13 Bab 10 : PHP MySQL Lanjut (Studi Kasus)

CSCI 4000 Assignment 4

MYSQL DATABASE ACCESS WITH PHP

Server side scripting and databases

webmonkey/programming/ PHP/MySQL Tutorial by Graeme Merrall Webmonkey: programming: PHP/MySQL Tutorial

Web Security. Attacks on Servers 11/6/2017 1

SQL stands for Structured Query Language. SQL lets you access and manipulate databases

the Data Drive IN THIS CHAPTER Good Things Come in Free Packages

Unit 27 Web Server Scripting Extended Diploma in ICT

Member registration and searching(2)

Greetings West Ada Comp Sci TAC & Friends,

You can use Dreamweaver to build master and detail Web pages, which

Unit - III. 3.1 Introduction. Settingup an Account

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

CS4604 Prakash Spring 2016! Project 3, HTML and PHP. By Sorour Amiri and Shamimul Hasan April 20 th, 2016

Database Systems. phpmyadmin Tutorial

EXPERIMENT- 9. Login.html

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

Secure Web-Based Systems Fall Test 1

Computing Science. Advanced Higher. Finalised Marking Instructions

WEB PROGRAMMING SCV1223. PHP : Authentication Example. Dr. Md Sah bin Hj Salam En. Jumail bin Taliba

APPENDIX. dbskripsi.sql CREATE DATABASE drop database if exists dbskripsi; create database dbskripsi; use dbskripsi;

A SQL Injection : Internal Investigation of Injection, Detection and Prevention of SQL Injection Attacks

php Mr. Amit Patel Hypertext Preprocessor Dept. of I.T.

CSCI 4000 Assignment 5

Simple sets of data can be expressed in a simple table, much like a

Retrieving Query Results

Transcription:

Database Connectivity using PHP Some Points to Remember: 1. PHP has a boolean datatype which can have 2 values: true or false. However, in PHP, the number 0 (zero) is also considered as equivalent to False. Every other value (1, 2, 17, -34, +1742, etc) is considered as True. In other words, zero is False and non-zero is True. This means that if we write: if ( 0)... It will evaluate to False, and the statements in the body of IF will not be executed. And if we write: if (1)... It will evaluate to True, and the statements in the body of the IF will get executed. Further, a function that returns zero is considered to have returned false. And a function that returns 1 or a non-zero value, is considered to have returned true. 2. The die() function is used to stop execution of a php program. It is similar to the return statement that you have seen in the class. This function simply checks the value of the argument we pass to it, and if the argument is false, it stops the php program (ie, the php page dies). Example: $conn = mysqli_connect("localhost","root","toor"); Here, the first line tries to connect to a MySql server using mysqli_connect() function. If the connection cannot be established, the function will return false which gets stored in a variable called $conn. Then we use an if condition to check if the value of $conn is false. If it is false, then we use the die() function to display an error message and immediately terminate execution of the php page. 3. To connect to a MySQL Database Server using PHP, there are two techniques: a. The MYSQL extension b. The MYSQLI extension The mysql extension was used in earlier versions of PHP but has now been deprecated meaning that its use is officially discouraged and it will be removed in the near future from PHP. There are only small differences in syntax when using either extension. You have seen programs that use functions from both mysql extension and mysqli extension. Here, all the programs will use the mysql extension. You should remember that some new versions of PHP

will show a warning when the page runs, telling you that you should use the new mysqli extension to connect to the database. We can suppress that warning by using the statement: error_reporting(e_all ^ E_DEPRECATED) at the top of each page. If we use mysqli, this statement is not needed. Database Connectivity Programs: 1. Write a PHP Program to Connect to a MySQL Server. Assume that the MySQL Server is running on the local machine and that your MySQL username is root with password toor. After connecting to the MySQL Server, open a database called test present on the server. Program to Connect to MySQL Server + Open a Database named "test" // If we are here, it means connection succeeded echo('successfully connected to mysql server <br/>'); $openstatus = mysql_select_db("test"); // If we are here, it means database was successfully opened echo('successfully opened the database named "test" <br/>'); 2. Write a PHP Program to create a new mysql database named Movies. Assume that your MySQL host is localhost, username is root and password is toor. Program to Create a New MySQL Database named Movies

$sql = "CREATE DATABASE Movies"; $result = mysql_query($sql); die('failed to create the database named "movies"<br/>'); else echo('database was created<br/>'); 3. Write a PHP Program to create a table called Students within the test database. Use same hostname, username and password to connect as in the previous question. The table you create should have the following structure: StudentID FirstName LastName DOB Int Varchar(30) Varchar(30) Date Program to Create new Table "Students" in the database named "test" // If we are here, it means connection succeeded echo('successfully connected to mysql server <br/>'); $openstatus = mysql_select_db("test");

// If we are here, it means database was successfully opened echo('successfully opened the database named "test" <br/>'); $sql = "Create Table Students(StudentID int, FirstName varchar(30), LastName varchar(30), DOB date)"; $result = mysql_query($sql); die('failed to create the Table named "Students" in database named "test"<br/>'); else echo('table was created<br/>'); 4. Suppose there exists a mysql database called test having a table students. Write a PHP Program to read and display all records from the table named students in a database named test on the MySQL Server. Program to retrieve all rows of a table and display them on the page $openstatus = mysql_select_db("test"); $result = mysql_query("select * From Students"); die('query failed <br/>'); while($row = mysql_fetch_row($result)) foreach($row as $x) echo($x);

echo(' '); echo('<br/>'); [output of program 4] 5. Suppose there exists a mysql database called test having a table Students whose structure is shown below: StudentID FirstName LastName DOB Int Varchar(30) Varchar(30) Date Write a PHP Program to display just the FirstName and LastName fields of those students whose StudentID is greater than 5. Program to Select and Display only those students whose ID is greater or equal to 5 $openstatus = @mysql_select_db("test"); $result = mysql_query("select FirstName,LastName From Students Where StudentID >= 5"); die('query failed <br/>'); while($row = mysql_fetch_row($result))

foreach($row as $x) echo($x); echo(' '); echo('<br/>'); 6. Write a PHP Program to insert a new student row in the Students table of the database named mydatabase. Assume the table has the following structure while writing your insert query: StudentID FirstName LastName DOB Int Varchar(30) Varchar(30) Date The Student record you enter should have the values as: StudentID FirstName LastName DOB 11 Muzamil Hassan 1997-12-23 Program to Insert a new row in the "Students" table of "test" database // If we are here, it means connection succeeded echo('successfully connected to mysql server <br/>'); $openstatus = mysql_select_db("test"); // If we are here, it means database was successfully opened echo('successfully opened the database named "test" <br/>'); $sql = "Insert Into Students(StudentID, FirstName, LastName, DOB) Values(11,'Muzamil','Hassan','1997-12-23')"; $result = mysql_query($sql);

table"<br/>'); else die('failed to Insert new Student Record in the "Students" echo('insert was successful<br/>'); 7. Write a PHP Program to create a mysql database whose name is entered by the user in a textbox. Use same hostname, username and passwords to connect to the mysql server as you have in the previous question. The PHP Page should look like this: This problem requires 2 pages: 4. An HTML Form that is shown to the user, where the user enters the name of the database and clicks the submit button (mysql_createdb_form.php) 5. A PHP script that receives the name of the database entered by the user on the HTML form and creates the database (mysql_createdb_script.php) mysql_createdb_form.php Program to Create a Database Whose Name is Entered by the User in a Textbox

<form method="post" action="mysql_createdb_script.php"> <h1>create Database Form</h1> <br/> Enter Database Name: <input type="text" name="txtdbname"> <input type="submit" value="create Database"> </form> mysql_createdb_script.php $database_name=$_post['txtdbname']; if(!$database_name) die('no Database Name Specified. Please try again'); $sql = "CREATE DATABASE ".$database_name; $result = mysql_query($sql); die('failed to create the database named '.$database_name.'<br/>'); else echo('database '.$database_name.' was created<br/>');

<a href="mysql_createdb_form.php">back TO FORM</a> *********