Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP PDO & PHP SOAP Introduce. Agenda What is PHP PDO and PHP SOAP? Setup PHP PDO to connect database, query database and close the connection. Setup SOAP.

Similar presentations


Presentation on theme: "PHP PDO & PHP SOAP Introduce. Agenda What is PHP PDO and PHP SOAP? Setup PHP PDO to connect database, query database and close the connection. Setup SOAP."— Presentation transcript:

1 PHP PDO & PHP SOAP Introduce

2 Agenda What is PHP PDO and PHP SOAP? Setup PHP PDO to connect database, query database and close the connection. Setup SOAP Server and SOAP Client.

3 What is PHP PDO? PDO (PHP Data Objects) is an interface for accessing databases in PHP To use a database we need to do 3 things: – Connect to the database – Query the database using SQL – Do something with result – Close the connection to the database In this slide, we use MySQL for demo. With another Database please see details in here.here

4 Connect to the database (1) See the figure below to know how PDO work with database

5 Connect to the database (2) Connect to MySQL database: – And we should always wrap your PDO operations in a try/catch as PDO uses exceptions to trap errors (there 3 modes PDO::ERRMODE_SILENT, PDO::ERRMODE_WARNING, PDO::ERRMODE_EXCEPTION)

6 Connect to the database (3) Setting error mode: – setAttribute(PDO::ATTR_ERRMODE,[Error mode]);?> List of error modes: – PDO::ERRMODE_SILENT: Just set error codes (it is the default mode) – PDO::ERRMODE_WARNING: Raise warnings – PDO::ERRMODE_EXCEPTION: Throw exceptions

7 Query the database using SQL (1) See the figure below to know the steps we must follow to avoid some issue when work with database.

8 Query the database using SQL (2) Prepare a query. We have some ways to binding data. – prepare('INSERT INTO user(username,password,email) VALUES (?,?,?)'); $data = array('test_user','test_pasword','test_email@test.com'); $st->execute($data);?> – prepare('INSERT INTO user(username,password,email) VALUES (:username,:password,:email)'); $data = array('username'=>'test_user', 'password'=>'test_pasword', 'email'=>'test_email@test.com'); $st->execute($data);?> – (...)

9 Query the database using SQL (3) If you want to bind data that it of a different data type (such as integer, boolean or lob), you can use bindParam() method.bindParam() – For 3 rd parameter default is PDO::PARAM_STRING: $query- >bindParam(':username', Test_Username'); – $query->bindParam(':id', $id, PDO::PARAM_INT) You can use exec() method to query but if you want to use prepare() method you must use execute() method.

10 Do something with result To get data you can use fetch() method to get the next row from a result set or fetchAll() method to get an array containing all of the result set rows.fetch()fetchAll() – query('SELECT * FROM test_table'); $result = $query->fetchAll([fetch_style]);?> We have some fetch styles: – PDO::FETCH_BOTH (it is the default): returns an array indexed by both column name and 0-indexed column number as returned in your result set – PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set – PDO::FETCH_OBJ: returns an object – (and another style please see in here)here

11 Close the connection to the database Close the connection – – We just set the handle to null – PDO does support persistent connections.

12 PHP SOAP and something you must know first SOAP (Simple Object Access Protocol). is a protocol for accessing web services. It is based on XML. WSDL (Web Services Description Language) is a language for describing web services and how to access them. It is written in XML. XML (eXtensible Markup Language) is designed to transport and store data. In this slide, we will create SOAP Server and SOAP Client with PHP.

13 The WSDL Document Structure (1) The main structure of WSDL document:

14 The WSDL Document Structure (2) Elements in WSDL – : this element is the most important WSDL element. It describes a web service, the operations that can be performed, and the messages that are involved. – : this element defines the data elements of an operation. – : this element defines the data types that are used by the web service – : this element defines the data format and protocol for each port type – : this element defines the ports supported by the Web service. For each of the supported protocols, there is one port element. The service element is a collection of ports.

15 PHP SOAP Server Methods for SoapServer class: – addFunction(): Adds one or more functions to handle SOAP requests – addSoapHeader: Add a SOAP header to the response – handle(): Handles a SOAP request – setClass(): Sets the class which handles SOAP requests – setObject(): Sets the object which will be used to handle SOAP requests – (and another method. To see details, please go here)here

16 PHP SOAP Client Methods for SoapClient class: – __soapCall(): Calls a SOAP function – __setSoapHeaders(): Sets SOAP headers for subsequent calls – __getFunctions(): Returns list of available SOAP functions – __getLastRequest(): Returns last SOAP request – __getLastRequestHeaders(): Returns the SOAP headers from the last request – __getLastResponse(): Returns last SOAP response – __getLastResponseHeaders(): Returns the SOAP headers from the last response – __doRequest(): Performs a SOAP request

17 DEMO

18 Demo source

19 Q&A


Download ppt "PHP PDO & PHP SOAP Introduce. Agenda What is PHP PDO and PHP SOAP? Setup PHP PDO to connect database, query database and close the connection. Setup SOAP."

Similar presentations


Ads by Google