MySQL Web Application Connecting to a MySQL database

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 taming PHP Thomas Krichel
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.
PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
PHP Hypertext Preprocessor Information Systems 337 Prof. Harry Plantinga.
Web Database Programming Connecting Database to Web.
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
Website Development Working with MySQL. What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
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.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Thursday, October 18, 2012 Session 7: PHP.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
LIS651 lecture 7 PHP mySQL Thomas Krichel
INTERNET APPLICATION DEVELOPMENT For More visit:
Create an online booking system (login/registration)
Server-side Scripting Powering the webs favourite services.
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.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
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.
MySQL Databases & PHP Integration Using PHP to write data to, and retrieve data from, a MySQL database.
PHP Part 2.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
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.
CS 174: Web Programming September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
2010/11 : [1]PHP with MySQLBuilding Web Applications using MySQL and PHP (W1) PHP with MySQL.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
PHP and SQL Server: Connection IST2101. Typical web application interaction (php, jsp…) database drivers 2IST210.
U:/msu/course/cse/103 Day 21, Slide 1 CSE 103 Makeups –If you didn’t take one over the weekend, take one TUESDAY or WEDNESDAY!
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 10 PHP MySQL Database
CSC 2720 Building Web Applications Accessing MySQL from PHP.
CSC 405: Web Application Engineering II8.1 Web programming using PHP What have we learnt? What have we learnt? Underlying technologies of database supported.
MySQL MySQL and PHP – interacting with a database.
13 – PHP MySQL Connection Informatics Department Parahyangan Catholic University.
PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative.
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.
Web Systems & Technologies
Web Database Programming Using PHP
PHP (Session 2) INFO 257 Supplement.
PHP Built-In Functions
PHP: MySQL Lecture 14 Kanida Sinmai
Introduction to Dynamic Web Programming
IS1500: Introduction to Web Development
Web Database Programming Using PHP
Web Design and Development
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
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
ISC440: Web Programming 2 Server-side Scripting PHP 3
Web Systems Development (CSC-215)
Introduction to Web programming
MySQL Web Application Connecting to a MySQL database
CIS 388 Internet Programming
PHP and MySQL.
PHP Hüseyin GÜNEŞ, 2018.
Web Programming Language
Tutorial 6 PHP & MySQL Li Xu
PHP Forms and Databases.
Introduction to Web programming
Conection
PHP Programming Using Cloud 9 IDE.
Presentation transcript:

MySQL Web Application Connecting to a MySQL database Building and Executing a Query Implement three examples

mysqli class for Connecting to a MySQL Database To access the data on a MySQL server, a connection to that server must be created. mysqli is a class that uses a data object to establish that connection. mysqli is an extension of MySQL and is short for MySQL Improved. mysqli is a relational database driver used in the PHP programming language to provide an interface with MySQL databases.

Connection Requirements Parameter $servername - Specifies the server to connect to. NOTE: If you pass the NULL value or an empty string "", the server will use the default value: "localhost” - $username - Specifies the MySQL username to log in with. NOTE: Default value is the name of the user that owns the server process - $password - Specifies the password to log in with. - $database - Specifies the database to be used when performing queries. - $port - Specifies the port number to attempt to connect to the MySQL server. NOTE: The default port is 3306

Example <?php // TASK 1: CONNECT TO THE SERVER $mysqli= new mysqli($servername, $username, $password, $database, $port);   // TASK 2: CHECK IF THERE IS AN ERROR CONNECTING. //Mysqli_connect_errno() WILL RETURN AN ERROR CODE. if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } //TASK 3: CLODE THE CONNECTION $mysqli->close(); ?>

PHP Documentation Site Example <?php // Connecting to and selecting a MySQL database named sakila // Hostname: 127.0.0.1 $mysqli = new mysqli('127.0.0.1', $username, $password, ‘sakila’); // Check if a connect_errno exists, causing a connection attempt to fail. if ($mysqli->connect_errno) { // The connection failed. What do you want to do? // You could contact yourself (email?), log the error, show a nice page, etc. // You do not want to reveal sensitive information echo "Sorry, this website is experiencing problems."; // Something you should not do on a public site, but this example will show you // anyways, is print out MySQL error related information -- you might log this echo "Error: Failed to make a MySQL connection, here is why: \n"; echo "Errno: " . $mysqli->connect_errno . "\n"; echo "Error: " . $mysqli->connect_error . "\n"; // In case of an error, exit exit; }

Perform an SQL Query $sql = "SELECT actor_id, first_name, last_name FROM actor WHERE actor_id = $aid"; if (!$result = $mysqli->query($sql)) { //Respond to a failed query. echo "Sorry, the website is experiencing problems."; // get the error information echo "Error: Our query failed to execute: \n"; echo "Query: " . $sql . "\n"; echo "Errno: " . $mysqli->errno . "\n"; echo "Error: " . $mysqli->error . "\n"; exit; }

Fetch the result of SQL Query // Our MySQL connection and query succeeded, but do we have a result? if ($result->num_rows === 0) { echo "We could not find a match, sorry about that. Please try again."; exit; } // Fetch the result into an associated array where the array's keys are the // table's column names $actor= $result->fetch_assoc(); echo "Sometimes I see " . $actor['first_name'] . " " . $actor['last_name'] . " on TV."; // Fetch five random rows and output their names to a list. $sql = "SELECT actor_id, first_name, last_name FROM actor ORDER BY rand() LIMIT 5"; if (!$result = $mysqli->query($sql)) { echo "Sorry, the website is experiencing problems."; // Print our 5 random names in a list, and link to each actor echo "<ul>\n"; while ($actor = $result->fetch_assoc()) { echo "<li><a href='".$_SERVER['SCRIPT_FILENAME']. "?aid=".$actor['actor_id']."'>\n"; echo $actor['first_name'] . ' ' . $actor['last_name']; echo "</a></li>\n"; echo "</ul>\n";

Close a MySQL Connection // The PHP script will automatically free the result // and close the MySQL connection when it exits. //Perform this task as a precaution. $result->free(); $mysqli->close();

Lab 12: Database

Lab12a: Display all Contact records <?php //TASK 1: MAKE A CONNECTION TO THE DATABASE, // DISPLAY ERROR FOR FAILED CONNECTIONS //TASK 2: BUILD A STRING CONTAININ A MYSQL INSTRUCTION. // SELECT ALL RECORDS //TASK 3: USE THE ESTABLISHED DATABASE CONNECTION TO // PROCESS THE DATABASE QUERY. STORE THE RESULTS IN A VARIABLE. // TASK 3: OUTPUT DATA FOR EACH ROW ?>

Lab12b: Construct a Query for all Records with a Specified First name <?php //TASK 1: MAKE A CONNECTION TO THE DATABASE //TASK 2: GET FIRST NAME FROM THE FORM //TASK 3: CONSTRUCT A QUERY FOR ALL RECORDS WITH A MATCHING FIRST NAME //TASK 4: DISPLAY ALL RECORDS FROM THE QUERY RESULT ?>

Lab12 c: Build a Table of Results to Send to JavaScript to display in HTML <?php //TASK 1: MAKE A CONNECTION TO THE DATABASE, // DISPLAY ERROR FOR FAILED CONNECTIONS //TASK 2: BUILD A QUERY STRING //TASK 3: PROCESS THE DATABASE QUERY. STORE THE RESULTS IN A VARIABLE. //TASK 4: BUILD A TABLE OF RESULTS IN A STRING ?>