Server-Side Application and Data Management IT IS 3105 (FALL 2009)

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.
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.
PHP and MySQL PHP for the Web, page PHP and MySQL MySQL Resource PHP – MySQL Resource
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
Website Development Working with MySQL. What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
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
© 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.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
Sayed Ahmed Computer Engineering, BUET, Bangladesh MSC, Computer Science, U of Manitoba, Canada
In the next lectures you will learn  What is SQL  How to access mySQL database  How to create a basic mySQL database  How to use some basic queries.
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.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
What is MySQLi? Since the mid-90s, Mysql extension has served as the major bridge between PHP and MySQL. Although it has performed its duty quite well,
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.
PHP Part 2.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Lecture 10 – MYSQL and PHP (Part 2)
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
PHP with MySQL 1.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
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.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
2010/11 : [1]PHP with MySQLBuilding Web Applications using MySQL and PHP (W1) PHP with MySQL.
PHP Database connectivity Connecting with RDBMS and editing, adding, and deleting databases therein are all done through PHP functions.
PHP Programming. Topics Database Handling (MySQL, MSSQL, ODBC)
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.
LECTURE 3 MYSQL Database. PHP MYSQL CONNECTION MySQL is a database system used on the web MySQL is a database system that runs on a server MySQL is ideal.
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
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.
CS242 SQL. What is SQL? SQL:  stands for Structured Query Language  allows you to access a database  is an ANSI standard computer language  can execute.
Nic Shulver, Storing Data on the Server Introduction We are going to look at some working code It writes fixed data into a simple.
Web Systems & Technologies
PHP (Session 2) INFO 257 Supplement.
PHP Built-In Functions
PHP: MySQL Lecture 14 Kanida Sinmai
PHP WORKSHOP (Session 2)
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Unix System Administration
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
Introduction to Web programming
COMP519: Web Programming Autumn 2015
MySQL Web Application Connecting to a MySQL database
Comp 519: Web Programming Autumn 2015
PHP Hüseyin GÜNEŞ, 2018.
Web Programming Language
Web Programming– UFCFB Lecture
MySQL Web Application Connecting to a MySQL database
Introduction to Web programming
Conection
Presentation transcript:

Server-Side Application and Data Management IT IS 3105 (FALL 2009) PHP and MySQL (Cont.) Mohamed Shehab Lecture 6

Setting up the Connection The PHP library for connecting to MySQL is called mysqli, where the i stands for improved. To connect to the MySQL server: $hostname = “localhost”; $username = “root”; $password = “123456”; $dbname = “mydb”; $db = new mysqli($hostname, $username, $password, $dbname); This line instantiate the mysqli class and creates a connection to the localhost and database with the specified username, and password.

Setting up the Connection The result of your attempt to connect is checked using the $db->connect_error value: $db = new mysqli($hostname, $username, $password, $dbname); if($db->connect_error){ echo ‘Error:’ . $db->connect_error; exit; }

Querying the Database Prepare a query string, then call the mysqli query method: $query = “select * from students”; $result = $db->query($query); In case of SELECT the query method returns a result set and false if there is an error. The result set can be accessed in multiple ways.

Querying the Database Accessing the result set: Method Description $result->fetch_assoc() Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset. $result->fetch_row() Fetches one row of data from the result set and returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero). $result->fetch_object() Returns the current row result set as an object where the attributes of the object represent the names of the fields found within the result set.

$result->fetch_assoc() while($row = $result->fetch_assoc()){ echo $row[“SID”] . “, “; echo $row[“fname”] . “, “; echo $row[“lname”] . “, “; echo $row[“age”] . “<br>“; }

$result->fetch_row() while($row = $result->fetch_row()){ echo $row[0] . “, “; echo $row[1] . “, “; echo $row[2] . “, “; echo $row[3] . “<br>“; }

$result->fetch_object() while($row = $result->fetch_object()){ echo $row->SID . “, “; echo $row->fname . “, “; echo $row->lname . “, “; echo $row->age. “<br>“; }

Important result set attributes and methods Attribute/Method Description $result->num_rows Returns the number of rows in the result set. $result->field_count Returns the number of fields from specified result set. $result->data_seek ( int $offset ) Seeks to an arbitrary result pointer specified by the offset in the result set. The offset must be between (0 to result->num_rows -1) $result->free() Frees the memory associated with a result

Inserting into the Database Prepare a query string, then call the mysqli query method: $query = “insert into students (SID, fname, lname, age, dept) values (‘800456783’,‘Bob’,’Smith’,25,’SIS’)”; $result = $db->query($query); In case of INSERT the query method returns true if the insert is successful and false if there is an error.

Inserting into the Database Checking the result status after an insert: if ($result) { echo $db->affected_rows." students inserted into database."; } else { echo "An error has occurred. The item was not added."; }

Other Database Object Methods and Attributes $db->affected_rows Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query. For SELECT statements returns the number of returned rows. $db->close() Closes a previously opened database connection For a detailed description of mysqli class check: http://us2.php.net/manual/en/mysqli.summary.php

Using Prepared Statements Mysqli library supports the use of prepared statements. Prepared statements are useful for speeding up execution when you are performing large numbers of the same query with different data. They also protect against SQL injection-style attacks.

Using Prepared Statements $query = "insert into students (SID, fname, lname, age, dept) values(?, ?, ?, ?, ?)"; $stmt = $db->prepare($query); $stmt->bind_param("sssds", $sid, $fname, $lname, $age, $dept); $sid = “800999234”; $fname=“Bob”; $lname=“Smith”; $age=23; $dept=“SIS”; $stmt->execute(); echo $stmt->affected_rows.' students inserted into database.'; $stmt->close(); Char Description i Variable has type integer d Variable has type double s Variable has type string b Variable has type blob