PHP with MySQL 1.

Slides:



Advertisements
Similar presentations
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.
Advertisements

PHP SQL. Connection code:- mysql_connect("server", "username", "password"); Connect to the Database Server with the authorised user and password. Eg $connect.
Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
MySQL. To start go to Login details: login: labuser password:macimd15 – There.
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.
Quick-and-dirty.  Commands end in a semi-colon ◦ If you forget, another prompt line shows up  Either continue the command or…  End it with a semi-colon.
PHP and MySQL. Why Use a Database  Easy access to data  Simultaneous access by multiple users is handled properly  Security - easy to control access.
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
MySQL and PHP By Trevor Adams.
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.
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
PHP : Working with Resultsets. Last class  Open a connection to the MySQL server.  Specify the database  Issue queries (no updates at this stage) 
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
LIS651 lecture 7 PHP mySQL Thomas Krichel
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
Sayed Ahmed Computer Engineering, BUET, Bangladesh MSC, Computer Science, U of Manitoba, Canada
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
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.
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.
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.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Lecture 10 – MYSQL and PHP (Part 2)
Technology & Management Club Development Software Overview.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
Database Access with PHP and MySQL CS356 Examples from Web Database Applications, by Hugh E. Williams & David Lane, O'Reilly, 2002.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
Session 7: Getting PHP to Talk to MySQL. Objectives Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data.
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
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.
2010/11 : [1]PHP with MySQLBuilding Web Applications using MySQL and PHP (W1) PHP with MySQL.
Enterprise PHP – Reading Data from a DB Reading Data from a relational database in PHP Nic Shulver, FCES, Staffordshire University Using the SQLi interface.
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.
>> 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.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
MySQL MySQL and PHP – interacting with a database.
PHP with MYSQL Please use speaker notes for additional information!
 2 Data Object Library approaches ◦ DAO (Data Access Objects)  Original access strategy (up to VB6)  Closely linked to MS Access ◦ ADO (ActiveX Data.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
PHP (Session 2) INFO 257 Supplement.
PHP: MySQL Lecture 14 Kanida Sinmai
Review 3 csc242 – web programming.
Chapter 5 Introduction to SQL.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
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
Objectives Connect to MySQL from PHP Learn how to handle MySQL errors
ISC440: Web Programming 2 Server-side Scripting PHP 3
PHP: Database connection
MySQL Web Application Connecting to a MySQL database
Database Access with PHP and MySQL
Introduction to Web programming
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
Presentation transcript:

PHP with MySQL 1

Basic PHP MySQL functions Connecting to a Database Selecting a database Running a query Using results of a query Closing the connection Original functions start with mysql_ Improved version from php5 starts with mysqli_

Database Connection mysqli_connect(server, username, password) server default is the string "localhost" username is a string for the user name ("labuser") password is a string for the password ("macimd15") e.g. For Mac lab $conn= mysqli_connect("localhost", "labuser", "macimd15"); For Windows Labs $conn= mysqli_connect("localhost", "labuser", "Labuser1"); For WAMP/MAMP/XAMPP with default password $conn= mysqli_connect("localhost", "root", "");

Sample Connection Code <?php $conn = mysqli_connect("localhost", "labuser", "macimd15") or die("Could not connect: " . mysqli_error($conn)); print "Successful Connection"; mysqli_close($conn); ?>

Selecting a database mysqli_select_db(connection , name) select a database given by the string name The connection variable is required e.g. mysqli_select_db($conn, "labuser");

Connect & Select database code <?php $conn = mysqli_connect('localhost', 'labuser', 'macimd15') or die ('No connection : ' . mysqli_error($conn)); mysqli_select_db($conn , 'labuser') or die ('db will not open' . mysqli_error($conn)); print "Database Connected"; mysqli_close($conn); ?>

Error Messages mysqli_error(connection) Returns an error string or error number (connection is optional - with last opened connection used if none supplied) Empty string is returned if there is no error. Example mysqli_error($conn);

Making A Query mysqli_query(connection , query) makes a select query query is a string for the MySQL query (in SQL) semicolon (;)should NOT be used to terminate query query uses valid SQL command e.g. $query = "SELECT surname FROM student"; $result = mysqli_query($conn, $query);

Connect, Select db & query <?php $conn = mysqli_connect('localhost', 'labuser', 'macimd15') or die ('No connection'); mysqli_select_db($conn ,'labuser') or die ('db will not open'); $query = "SELECT surname FROM student"; $result = mysqli_query($conn, $query) or die("Invalid query"); print "Successful Query"; mysqli_close($conn); ?>

Closing a connection mysqli_close(connection) closes the database connection with the link e.g. mysqli_close($conn); mysqli_free_result(result) frees up memory during a program mysqli_free_result($result);

Other mysql Functions (1) mysqli_num_rows(result) returns number of rows from a select query mysqli_fetch_row(result) each call returns the next row as an indexed array e.g. $result = mysqli_query($conn, "select * from module"); $num = mysqli_num_rows($result); for($i=1; $i<=$num; $i++) { $row = mysqli_fetch_row($result); echo $row[0] . " " . $row[1]; }

Other mysql Functions (2) mysqli_affected_rows(result) used after an INSERT, UPDATE, or DELETE query to return the number of rows affected mysqli_free_result(result) frees memory within a php program

Other mysql Functions (3) mysqli_num_fields(result) returns the name of the table column whose position is given by index (0,1,...) e.g. $res = mysqli_query($conn, 'select  *  from module'); $numfields = mysqli_num_fields($res); echo "number of fields in module is " . $numfields;

Other mysql Functions (4) mysqli_fetch_array(result) returns row information as an array e.g. $result = mysqli_query($conn, "select * from module"); while($row = mysqli_fetch_array($result)) { echo $row['modulecode'] . " " . $row['modulename']; } Function returns TRUE while a row is fetched & FALSE when there are no rows available Function will fetch an array as associative or sequential

Sample Code 1 <?php $conn = mysqli_connect('localhost', 'root', '') or die ('No connection'); mysqli_select_db($conn, 'projemp') or die ('DB will not open'); $query = "SELECT eno, ename, salary from emp order by ename asc"; $result = mysqli_query($conn, $query) or die("Invalid query"); $num = mysqli_num_rows($result); echo "<table border='1'><tr><th>eno</th><th>ename</th> <th>salary</th></tr>"; for($i=0; $i<$num; $i++) { $row = mysqli_fetch_row($result); echo "<tr><td>" . $row[0] . "</td><td>" . $row[1]; echo "</td><td>" . $row[2] . "</td></tr>"; } echo "</table>"; mysqli_close($conn); ?>

For Loop //counts number of rows in $result $num = mysqli_num_rows($result); for($i=0; $i<$num; $i++) { // goes round once for each row $row = mysqli_fetch_row($result); // puts next row into $row array echo "<tr><td>" . $row[0] . "</td><td>" . $row[1]; echo "</td><td>" . $row[2] . "</td></tr>"; } // prints the first, second & third column of the current row // as sequential array values … 0,1,2

Sample Code 2 <?php $conn = mysqli_connect('localhost', 'root', '') or die ('No connection'); mysqli_select_db($conn,'projemp') or die (' DB will not open'); $query = "SELECT eno, ename, salary from emp order by ename desc"; $result = mysqli_query($conn, $query) or die('Invalid query'); echo "<table border='1'><tr><th>eno</th><th>ename</th> <th>salary</th></tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr><td>" . $row['eno'] . "</td><td>" . $row['ename']; echo "</td><td>" . $row['salary'] . "</td></tr>"; } echo "</table>"; mysqli_close($conn); ?>

While Loop // while there is a new row to fetch while($row = mysqli_fetch_array($result)) { echo "<tr><td>" . $row['eno'] . "</td><td>" . $row['ename']; echo "</td><td>" . $row['salary'] . "</td></tr>"; } // print the columns using associative array index values // 'eno', 'ename' & 'salary'

Next Step Having connected to mysql (mysqli_connect) Selected a database (mysqli_select_db) Run a query (mysqli_query) Process the result (mysqli_fetch_row, mysqli_fetch_array) Generalise the select query Get selection clause value from the user (textbox, or even better, a drop down box in html form)

Adding variable to a query string $value=$_POST["valuelist"]; $query = "SELECT * from emp where salary > " . $value; [Whatever value is entered in the html front end is posted and plugged into the query string] Where the where clause uses a text attribute inverted commas must be placed round the value e.g. SELECT * FROM emp WHERE ename = 'smith' $query = "SELECT * from emp where ename = '" . $value . "'"; [Note the single quotes placed round the variable]

Generalised Query 1

Text Box In an html form: Enter a salary value: <input type = "text" name = "textvalue"> Backend php file dbconnect1.php: <?php $sal = $_POST["textvalue"]; …………………………. $query = "SELECT * from emp where salary > " . $sal;

dbconnect1.php <?php $sal=$_POST["textvalue"]; // receives posted value entered in textbox called textvalue $conn = mysqli_connect('localhost', 'labuser', 'macimd15') or die ('No connection'); mysqli_select_db($conn, 'labuser') or die (' test will not open'); $query = "SELECT eno, ename, salary from emp where salary > " . $sal; $result = mysqli_query($conn, $query) or die("Invalid query"); echo "<table border='1'><tr><th>eno</th><th>ename</th><th>salary</th></tr>"; while ($row = mysqli_fetch_row($result)) { echo "<tr><td>" . $row[0] . "</td><td>" . $row[1]; echo "</td><td>" . $row[2] . "</td></tr>"; } echo "</table>"; mysqli_close($conn); ?>

Generalised Query 1 output

Generalised Query 2

Select Box In an html form: <select name="valuelist"> <option value="24000">24000</option> <option value="28000">28000</option> <option value="32000">32000</option> </select> Chosen valuelist option value posted to backend php file (dbconnect2.php) $sal = $_POST["valuelist"]; $query = "SELECT * from emp where salary > " . $sal;

dbconnect2.php <?php $sal = $_POST["valuelist"]; // receives posted value option from selectbox valuelist $conn = mysqli_connect('localhost', 'labuser', 'macimd15') or die ('No connection'); mysqli_select_db($conn, 'labuser') or die (' test will not open'); $query = "SELECT eno, ename, salary from emp where salary > " . $sal; $result = mysqli_query($conn, $query) or die("Invalid query"); echo "<table border='1'><tr><th>eno</th><th>ename</th><th>salary</th></tr>"; while ($row = mysqli_fetch_row($result)) { echo "<tr><td>" . $row[0] . "</td><td>" . $row[1]; echo "</td><td>" . $row[2] . "</td></tr>"; } echo "</table>"; mysqli_close($conn); ?>

Summary Connect to MySQL server connection = mysqli_connect(server, username, password) Select a database mysqli_select_db(connection, databasename) Run a query result = mysqli_query(connection, query) Process query mysqli_fetch_row(result) mysqli_fetch_array(result) Close connection mysqli_close(connection)