{"id":237,"date":"2018-07-13T06:12:06","date_gmt":"2018-07-13T06:12:06","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=237"},"modified":"2018-08-13T04:33:42","modified_gmt":"2018-08-13T04:33:42","slug":"php-and-mysql","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/php-and-mysql\/","title":{"rendered":"PHP and MySQL"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0<strong>Ways to connect PHP and MySQL<\/strong>\r\n\r\n\u2022\u00a0<strong>Useful methods of PDO<\/strong>\r\n\r\n\u2022\u00a0<strong>PHP and MySQL connectivity<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Web Database Architecture<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Web Application is a client\/server architecture. In typical web application client\/server architecture consist of two objects: 1. A web browser and 2. A web server. Web Browser and Web Server communicate via a communication link \u2013 generally internet or through http protocol. A web browser send request to web server. The Web server send response to the web server. This architecture is used for a server sends static web pages. which Following figure shows the simple client\/server architecture.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-239\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-111.png\" alt=\"\" width=\"762\" height=\"226\" \/>\r\n\r\n&nbsp;\r\n\r\nThe typical web database application consists of following stages.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1.Using browser, user send request to webserver using HTTP. For example, by entering city name, the user wants to get all the vendor details who belongs to city. By entering city name in HTML form and click submit button, he sends request to webserver. The search result page is fetch.php<\/p>\r\n<p style=\"text-align: justify\">2. The web server \u2013 Apache \u2013 receives the request and retrieve the file fetch.php. The webserver passes the file to PHP engine for processing.<\/p>\r\n<p style=\"text-align: justify\">3. The PHP engine starts parsing the script. Inside the script, there is a command to connect to the database and execute the query. For example, here the query is to search vendor details based on their city name. PHP opens a connection to MySQL server and send query to MySQL server.<\/p>\r\n<p style=\"text-align: justify\">4. MySQL server receives the query, process it and send the result back to the PHP engine<\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">5. The PHP engine get the query result, finishes the script and formatting the query result in HTML. Then returns the resulting HTML to web server.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">6. The web server sends HTML back to the browser.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0The php engine and database server can run on the same machine or different machine.<\/p>\r\n&nbsp;\r\n\r\n<strong>Three ways to connect PHP and MySQL<\/strong>\r\n<ul>\r\n \t<li style=\"text-align: justify\">An Application Programming Interface (API) is used to communicate an application with other applications. MySQL provides an API which specifies how PHP can work with MySQL.<\/li>\r\n \t<li>PHP provides 3 extensions to work with MySQL API.<\/li>\r\n<\/ul>\r\n<ol>\r\n \t<li>MySQL extension<\/li>\r\n \t<li>MySQLi extension<\/li>\r\n \t<li>PDO MySQL driver extension<\/li>\r\n<\/ol>\r\n<ul>\r\n \t<li style=\"text-align: justify\">PDO (PHP Data Objects) is a database abstraction layer. It is used specifically for PHP applications. To communicate with MySQL, It uses PDO MySQL driver extension. PDO can use other drivers extension to communicate with other databases.<\/li>\r\n \t<li>Each extension has its own pros and cons.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>PDO (PHP Data Objects) Pros<\/strong>\r\n<ul>\r\n \t<li>Available for PHP 5.0 and later<\/li>\r\n \t<li>Object Oriented Interface<\/li>\r\n \t<li style=\"text-align: justify\">Consistent Interface which is portable between many database servers such as Oracle, DB2, MS SQL Server and Postgre SQL<\/li>\r\n \t<li>Use features available MySQL 4.1.3 and later<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0 \u00a0PDO Cons<\/strong>\r\n<ul>\r\n \t<li>Does not work PHP 4.X or earlier<\/li>\r\n \t<li>Not use advances features of MySQL 4.1.3 and later such as multiple statements<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>MySQLi (MySQL improved extension) Pros<\/strong>\r\n<ul>\r\n \t<li>Included with PHP 5.X or later<\/li>\r\n \t<li>Provides Object-Oriented and Procedure Oriented Interface<\/li>\r\n \t<li>use all advances features of MySQL 4.1.3 and later<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0 MySQLi Cons<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n<ul>\r\n \t<li>Cannot used with other database servers<\/li>\r\n<\/ul>\r\n<strong>\u00a0 \u00a0 MySQL (MySQL extension) pros<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0 Available for PHP 3.X, 4.X and 5.X\r\n\r\n&nbsp;\r\n\r\n<strong>MySQL cons<\/strong>\r\n<ul>\r\n \t<li>Not use advances features of MySQL 4.1.3 and later such as multiple statements<\/li>\r\n \t<li>Not under active development<\/li>\r\n<\/ul>\r\nPDO is used in this module for demonstration.\r\n<ul>\r\n \t<li style=\"text-align: justify\">To use PDO object for making connection with MySQL, you must create a PDO object with three arguments. The three arguments are \u2013 DSN, User Name and Password.<\/li>\r\n \t<li>The general syntax is:<\/li>\r\n<\/ul>\r\n<p style=\"text-align: center\">new PDO($dsn, $username, $password)<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify\">DSN is short form of Data Source Name. The DSN specifies host name and database name. If MySQL database runs on the same server as PHP, then host name is localhost. The syntax of DSN for MySQL database is<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nmysql:host=hostaddress;dbname=databasename\r\n\r\n&nbsp;\r\n\r\nor you can set the values in a variable.\r\n\r\n&nbsp;\r\n\r\n$dsn = 'mysql:host=localhost;dbname=account'\r\n<ul>\r\n \t<li style=\"text-align: justify\">To call a method of the object, type object name, followed by object access operator \u2013 code as -&gt; - and followed by method name with arguments required for method in parentheses. Multiple arguments are separated by comma. The general syntax is<\/li>\r\n<\/ul>\r\nobjectName-&gt;methodName(Argument1 [, Argument2 ..])\r\n<ul>\r\n \t<li style=\"text-align: justify\">To execute the select statement of SQL, query method of PDO object is used. The query method takes one parameter.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 95.0625px\"><strong>Method<\/strong><\/td>\r\n<td style=\"width: 533.063px\"><strong>Description<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 95.0625px\" rowspan=\"3\">query($select)\r\n\r\n&nbsp;\r\n\r\n&nbsp;<\/td>\r\n<td style=\"width: 533.063px\" rowspan=\"3\">Execute the $select statement and returns a PDO statement object. The PDO\r\n\r\nstatement object contains the result set of $select. If no result set is returned,\r\n\r\nthen it returns false value.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<ul>\r\n \t<li>The general syntax to use query method is\u00a0 $PDOobject-&gt;query($select)<\/li>\r\n \t<li style=\"text-align: justify\">PDOStatement class has many methods. Below table describe some methods of the PDOStatement Class<\/li>\r\n<\/ul>\r\n<table class=\"aligncenter\" style=\"height: 114px\" border=\"1\">\r\n<tbody>\r\n<tr style=\"height: 30px\">\r\n<td style=\"height: 30px;width: 53.0625px\"><strong>Method<\/strong><\/td>\r\n<td style=\"height: 30px;width: 502.063px\"><strong>Description<\/strong><\/td>\r\n<td style=\"height: 30px;width: 0.0625px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 53.0625px\" rowspan=\"2\">fetch( )\r\n\r\n&nbsp;<\/td>\r\n<td style=\"height: 56px;width: 502.063px\" rowspan=\"2\">Returns an array having result set for next row. Array can be index by\r\n\r\nstring and number. String is used for column name and number for column<\/td>\r\n<td style=\"height: 28px;width: 0.0625px\"><\/td>\r\n<\/tr>\r\n<tr style=\"height: 28px\">\r\n<td style=\"height: 28px;width: 0.0625px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<div>\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td><\/td>\r\n<td>position. If no array is available , it returns false.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>fetchall( )<\/td>\r\n<td>Returns an array having all the rows of the result set.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>rowcount( )<\/td>\r\n<td>Returns the number of rows affected by the executing of last statement.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">closeCursor( )\r\n\r\n&nbsp;<\/td>\r\n<td rowspan=\"2\">Close the cursor and frees the connection to the server so other SQL\r\n\r\nstatement can be issued.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<ul>\r\n \t<li style=\"text-align: justify\">The following code shows one example using the query and fetch method. There is a GUI which ask user to enter city name and click on submit button. The output is displayed all the details of vendor who lives in the city entered by user.<\/li>\r\n \t<li>The HTML code for GUI is shown below.<\/li>\r\n<\/ul>\r\n&lt;html&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form action=\"fetch.php\" method=\"post\"&gt; Enter City Name:\r\n\r\n&lt;input type=\"text\" name=\"city\"&gt; &lt;br&gt;&lt;br&gt;\r\n\r\n&lt;input type=\"submit\" name=\"submit\" value=\"Submit\"&gt; &lt;input type=\"reset\" name=\"reset\" value=\"Reset\"&gt;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n<ul>\r\n \t<li>The output of HTML code is shown below:<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-241 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-112.png\" alt=\"\" width=\"218\" height=\"112\" \/>\r\n<ul>\r\n \t<li>The PHP Code for file fetch.php is shown below<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$dsn = 'mysql:host=localhost;dbname=account'; $uname = 'root';\r\n\r\n$pwd = '';\r\n\r\n$db = new PDO($dsn,$uname,$pwd); $vcity = $_POST['city'];\r\n\r\n$query = \"select * from vendor\r\n\r\nwhere vcity = '$vcity'\";\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Query : $query &lt;br&gt;\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$result = $db-&gt;query($query);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$row = $result-&gt;fetch();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">while($row != null)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$vid = $row['vid'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$vname = $row['vname'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$vaddress = $row['vaddress'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$vcity = $row['vcity'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$vmobile = $row['vmobile'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt;\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Vendor ID : $vid\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Vendor Name: $vname\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Vendor Address: $vaddress\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Vendor City : $vcity\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Vendor Mobile : $vmobile\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$row = $result-&gt;fetch();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$result-&gt;closeCursor();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n<ul>\r\n \t<li>The output is given below:<\/li>\r\n \t<li>Query : select * from vendor where vcity = 'Rajkot'<\/li>\r\n<\/ul>\r\n<p style=\"text-align: left\">\u00a0 \u00a0 Vendor ID : 1<\/p>\r\n<p style=\"text-align: left\">Vendor Name: Anand<\/p>\r\n<p style=\"text-align: left\">Vendor Address: Ravi Park<\/p>\r\n<p style=\"text-align: left\">Vendor City : Rajkot<\/p>\r\n<p style=\"text-align: left\">Vendor Mobile : 9876034521<\/p>\r\n<p style=\"text-align: left\">Vendor ID : 2<\/p>\r\n<p style=\"text-align: left\">Vendor Name: Dharmesh<\/p>\r\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor Address: 19, Park Avenue<\/span><\/p>\r\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor City : Rajkot<\/span><\/p>\r\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor Mobile : 1234567890<\/span><\/p>\r\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor ID : 7<\/span><\/p>\r\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor Name: Manish<\/span><\/p>\r\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor Address: University Road<\/span><\/p>\r\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor City : Rajkot<\/span><\/p>\r\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor Mobile : 9087654321<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n<ul>\r\n \t<li style=\"text-align: justify\">In the above code, fetch() method is used. The same code be written using fetchall() method. Following code shows HTML and PHP code of the above program using fetchall() method.<\/li>\r\n \t<li>HTML code<\/li>\r\n<\/ul>\r\n&lt;html&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form action=\"fetchall.php\" method=\"post\"&gt;\r\n\r\nEnter City Name:\r\n\r\n&lt;input type=\"text\" name=\"city\"&gt;\r\n\r\n&lt;br&gt;&lt;br&gt;\r\n\r\n&lt;input type=\"submit\" name=\"submit\" value=\"Submit\"&gt;\r\n\r\n&lt;input type=\"reset\" name=\"reset\" value=\"Reset\"&gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n<ul>\r\n \t<li>PHP Code<\/li>\r\n<\/ul>\r\n&lt;?php\r\n\r\n$dsn = 'mysql:host=localhost;dbname=account'; $uname = 'root';\r\n\r\n$pwd = '';\r\n\r\n$db = new PDO($dsn,$uname,$pwd); $vcity = $_POST['city'];\r\n\r\n$query = \"select *\u00a0<span style=\"font-size: 1em;text-align: initial\">from vendor\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">where vcity = '$vcity'\";<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\/\/echo \"&lt;br&gt; Query : $query &lt;br&gt;\";\r\n\r\n$result = $db-&gt;query($query);\r\n\r\n$rows = $result-&gt;fetchall();\r\n\r\nforeach($rows as $row)\r\n\r\n{\r\n\r\n$vid = $row['vid'];\r\n\r\n$vname = $row['vname'];\r\n\r\n$vaddress = $row['vaddress'];\r\n\r\n$vcity = $row['vcity'];\r\n\r\n$vmobile = $row['vmobile'];\r\n\r\necho \"&lt;br&gt;\";\r\n\r\necho \"&lt;br&gt; Vendor ID : $vid\";\r\n\r\necho \"&lt;br&gt; Vendor Name: $vname\";\r\n\r\necho \"&lt;br&gt; Vendor Address: $vaddress\";\r\n\r\necho \"&lt;br&gt; Vendor City : $vcity\";\r\n\r\necho \"&lt;br&gt; Vendor Mobile : $vmobile\";\r\n\r\n}\r\n\r\n$result-&gt;closeCursor();\r\n\r\n?&gt;\r\n<ul>\r\n \t<li style=\"text-align: justify\">To execute insert, update or delete statement exec method of PDO object is used. Exec method takes insert, update or delete SQL statement as argument.<\/li>\r\n<\/ul>\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td>Method<\/td>\r\n<td>Description<\/td>\r\n<\/tr>\r\n<tr>\r\n<td rowspan=\"2\">exec($SQLSelect)\r\n\r\n&nbsp;<\/td>\r\n<td rowspan=\"2\">Execute the $select and returns a number indicate how many rows are\r\n\r\naffected. If no rows are affected, then return zero.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<ul>\r\n \t<li>For example, to insert a new record in vendor table first create GUI. Following code shows<\/li>\r\n<\/ul>\r\n&nbsp;\r\n<p style=\"text-align: justify\">&lt;html&gt;<\/p>\r\n<p style=\"text-align: justify\">&lt;body&gt;<\/p>\r\n<p style=\"text-align: justify\">&lt;form action='exec.php' method='post'&gt; &lt;table&gt;<\/p>\r\n<p style=\"text-align: justify\">&lt;!-- vid --&gt; &lt;tr&gt;<\/p>\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">&lt;td&gt;Vendor ID:&lt;\/td&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type='text' name='vid'&gt; &lt;\/td&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;!-- vname --&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt;Vendor Name:&lt;\/td&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type='text' name='vname'&gt; &lt;\/td&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;!-- vaddress\u00a0 --&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt;Vendor Address:&lt;\/td&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type='text' name='vaddress'&gt; &lt;\/td&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;!-- vcity --&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; Vendor City: &lt;\/td&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type='text' name='vcity'&gt; &lt;\/td&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;!-- vmobile --&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; Vendor Mobile: &lt;\/td&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type='text' name='vmobile'&gt; &lt;\/td&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;!-- submit and reset --&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type='submit' name='submit' value='Submit'&gt; &lt;\/td&gt; &lt;td&gt; &lt;input type='reset' name='reset' value='Reset'&gt; &lt;\/td&gt; &lt;\/tr&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/table&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\"><\/p>\r\nThe output of the HTML code is :\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-242 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-113.png\" alt=\"\" width=\"270\" height=\"202\" \/>\r\n\r\n<\/div>\r\n<ul>\r\n \t<li><span style=\"text-align: initial;font-size: 1em\">The PHP code for file exec.php is given below.<\/span><\/li>\r\n<\/ul>\r\n<div>\r\n\r\n\u00a0 \u00a0&lt;?php\r\n\r\n$dsn = 'mysql:host=localhost;dbname=account'; $uname = 'root';\r\n\r\n$pwd = '';\r\n\r\n$db = new PDO($dsn,$uname,$pwd);\r\n\r\n$vid = $_POST['vid'];\r\n\r\n$vname = $_POST['vname'];\r\n\r\n$vaddress = $_POST['vaddress']; $vcity = $_POST['vcity']; $vmobile = $_POST['vmobile'];\r\n\r\n$query = \"insert into vendor\r\n\r\nvalues($vid,'$vname','$vaddress','$vcity','$vmobile') \";\r\n\r\necho \"&lt;br&gt; Query : $query &lt;br&gt;\"; $result = $db-&gt;exec($query);\r\n\r\necho \"&lt;br&gt; Result is : $result\"; if($result &gt; 0)\r\n\r\n{\r\n\r\necho \"&lt;br&gt; Record Insrted Successfully\";\r\n\r\n}\r\n\r\n?&gt;\r\n<ul>\r\n \t<li>The output is<\/li>\r\n<\/ul>\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\">\u00a0 \u00a0Query : insert into vendor values(22,'Kalpesh','Bopal','Ahmedabad','9753124680')<\/p>\r\n<p style=\"text-align: center\">Result is : 1<\/p>\r\n<p style=\"text-align: center\">Record Insrted Successfully<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify\">For live application development, prepare method is used. Prepare method is a method of PDO object. Prepare statement improves database performance and security.<\/li>\r\n \t<li style=\"text-align: justify\">When a statement is executed multiple times, the statements allow the database server to reuse of the work which is done when prepare a statement.<\/li>\r\n \t<li style=\"text-align: justify\">Prepare statement also improves security because it can prevent most types of the SQL injection attacks.<\/li>\r\n<\/ul>\r\n<img class=\"alignnone size-full wp-image-243 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-114.png\" alt=\"\" width=\"682\" height=\"272\" \/>\r\n\r\nLet\u2019s take one example using prepare statement. The HTML code is shown below\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form action='prepareselect.php' method='post'&gt; Enter city name :\r\n\r\n&lt;input type=\"text\" name=\"vcity\"&gt; &lt;\/input&gt;\r\n\r\n&lt;br&gt;&lt;br&gt;\r\n\r\n&lt;input type='submit' name='submit' value='Submit'&gt; &lt;input type='reset' name='reset' value='Reset'&gt; &lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n<img class=\"size-full wp-image-244 aligncenter\" style=\"text-align: initial;font-size: 1em\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-115.png\" alt=\"\" width=\"236\" height=\"134\" \/>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0The PHP code for the file prepareselect.php is shown below.\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$dsn='mysql:host=localhost;dbname=account';\r\n\r\n$uname = 'root';\r\n\r\n$pwd = '';\r\n\r\n$db = new PDO($dsn,$uname,$pwd); $vcity = $_POST['vcity'];\r\n\r\n$query = \"select * from vendor\r\n\r\nwhere vcity = :vcity\";\r\n\r\n$result = $db-&gt;prepare($query);\r\n\r\n$result-&gt;bindValue(':vcity',$vcity); $result-&gt;execute();\r\n\r\n$rows = $result-&gt;fetchAll();\r\n\r\nforeach($rows as $r)\r\n\r\n{\r\n\r\necho '&lt;br&gt; Vendor ID :'. $r['vid'];\r\n\r\necho '&lt;br&gt; Vendor Name :'. $r['vname'];\r\n\r\necho '&lt;br&gt; Vendor Address :'. $r['vaddress'];\r\n\r\necho '&lt;br&gt; Vendor City :'. $r['vcity'];\r\n\r\necho '&lt;br&gt; Vendor Mobile :'. $r['vmobile'];\r\n\r\necho \"&lt;br&gt;\";\r\n\r\n}\r\n\r\n$result-&gt;closeCursor();\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nAs the user clicks on submit button, he\/she will get the output as shown below.\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 Vendor ID :1\r\n\r\nVendor Name :Anand\r\n\r\nVendor Address :Ravi Park\r\n\r\nVendor City :Rajkot\r\n\r\nVendor Mobile :9876034521\r\n\r\n&nbsp;\r\n\r\nVendor ID :2\r\n\r\nVendor Name :Dharmesh\r\n\r\nVendor Address :19, Park Avenue\r\n\r\nVendor City :Rajkot\r\n\r\nVendor Mobile :1234567890\r\n\r\n&nbsp;\r\n\r\nVendor ID :7\r\n\r\nVendor Name :Manish\r\n\r\nVendor Address :University Road\r\n\r\nVendor City :Rajkot\r\n\r\nVendor Mobile :9087654321\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the similar way, prepare and bindValue method can be used to insert,update or delete statement. Following code shows to insert a record using prpare and bindValue.<\/p>\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form action='prepareinsert.php' method='post'&gt;\r\n\r\n&lt;table&gt;\r\n\r\n&lt;!-- vid --&gt; &lt;tr&gt;\r\n\r\n&lt;td&gt;Vendor ID:&lt;\/td&gt;\r\n\r\n&lt;td&gt; &lt;input type='text' name='vid'&gt; &lt;\/td&gt; &lt;\/tr&gt;\r\n\r\n&lt;!-- vname --&gt; &lt;tr&gt;\r\n\r\n&lt;td&gt;Vendor Name:&lt;\/td&gt;\r\n\r\n&lt;td&gt; &lt;input type='text' name='vname'&gt; &lt;\/td&gt; &lt;\/tr&gt;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">&lt;!-- vaddress\u00a0 --&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;td&gt;Vendor Address:&lt;\/td&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type='text' name='vaddress'&gt; &lt;\/td&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;!-- vcity --&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; Vendor City: &lt;\/td&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type='text' name='vcity'&gt; &lt;\/td&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;!-- vmobile --&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; Vendor Mobile: &lt;\/td&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type='text' name='vmobile'&gt; &lt;\/td&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;!-- submit and reset --&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type='submit' name='submit' value='Submit'&gt; &lt;\/td&gt; &lt;td&gt; &lt;input type='reset' name='reset' value='Reset'&gt; &lt;\/td&gt; &lt;\/tr&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/table&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span>\r\n\r\n<\/div>\r\n<div><\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-245 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-116.png\" alt=\"\" width=\"240\" height=\"212\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nThe code for prepareinsert.php is shown below.\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0&lt;?php<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$dsn='mysql:host=localhost;dbname=account';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$uname = 'root';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$pwd = '';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$db = new PDO($dsn,$uname,$pwd);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$vid = $_POST['vid'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$vname = $_POST['vname'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$vaddress = $_POST['vaddress'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$vcity = $_POST['vcity'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$vmobile = $_POST['vmobile'];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$query = \"insert into vendor values (:vid, :vname, :vaddress, :vcity, :vmobile)\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$result = $db-&gt;prepare($query);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$result-&gt;bindValue(':vid',$vid);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$result-&gt;bindValue(':vname',$vname);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$result-&gt;bindValue(':vaddress',$vaddress);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$result-&gt;bindValue(':vcity',$vcity);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$result-&gt;bindValue(':vmobile',$vmobile);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$success = $result-&gt;execute();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if($success &gt; 0)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; Record Inserted Successfully \";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$result-&gt;closeCursor();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0Output for the program is<\/span>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"font-family: 'Cormorant Garamond', serif;font-size: 1.66667em\">Record Inserted Successfully<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n<ul>\r\n \t<li style=\"text-align: justify\">An exception is an object which contains information about error which has occurred. Some PHP statements throw exception when an error is occurred. If an exception is not handled, then the applications terminate with error. To handle exception, try catch statement is used.<\/li>\r\n \t<li>To handle the errors thrown by the PDO library, the PDO Exception class is used.<\/li>\r\n \t<li>The below code shows an example of PDO Exception.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 &lt;?php\r\n\r\n\/\/ PDO Database Object created\r\n\r\n$dsn ='mysql:host=localhost;dbname=account';\r\n\r\n$uname = 'root';\r\n\r\n$pwd = 'pwd';\r\n\r\ntry\r\n\r\n{\r\n\r\n$db = new PDO($dsn,$uname,$pwd);\r\n\r\necho \"&lt;br&gt; Connection established successfully\";\r\n\r\n\/\/\r\n\r\n$query = \"select * from vendor\";\r\n\r\necho '&lt;br&gt;'. $query . '&lt;br&gt;';\r\n\r\n$rows = $db-&gt;query($query);\r\n\r\n$row = $rows-&gt;fetch();\r\n\r\nwhile ($row != null)\r\n\r\n{\r\n\r\n$vid = $row['vid'];\r\n\r\n$vname = $row['vname'];\r\n\r\n$vaddress = $row['vaddress'];\r\n\r\n$vcity = $row['vcity'];\r\n\r\n$vmobile = $row['vmobile'];\r\n\r\necho '&lt;br&gt;';\r\n\r\necho \"vid : $vid &lt;br&gt;\";\r\n\r\necho \"vname : $vname &lt;br&gt;\";\r\n\r\necho \"vaddress : $vaddress &lt;br&gt; \";\r\n\r\necho \"vcity : $vcity &lt;br&gt; \";\r\n\r\necho \"vmobile : $vmobile &lt;br&gt; \";\r\n\r\n\/\/ fetch next record\r\n\r\n$row = $rows-&gt;fetch();\r\n\r\n}\r\n\r\n}\r\n\r\ncatch(PDOException $e)\r\n\r\n{\r\n\r\n$errormessage = $e-&gt;getMessage();\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo \"&lt;br&gt; An error occured when connecting with database. The error is :<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$errormessage\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span>\r\n\r\n<\/div>\r\n<ul>\r\n \t<li>The output of the program is<\/li>\r\n<\/ul>\r\n&nbsp;\r\n<p style=\"text-align: justify\">An error occured when connecting with database. The error is : SQLSTATE[28000]<\/p>\r\n[1045] Access denied for user 'root'@'localhost' (using password: YES)\r\n\r\n&nbsp;\r\n\r\n<strong>References:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\r\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\r\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass:Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\r\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O'Reilly Media<\/p>\r\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove: Professional PHP,Wrox<\/p>\r\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\r\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\r\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\r\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\r\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, Pearson Education<\/p>\r\n<p style=\"text-align: justify\">11. http:\/\/www.php.net\/<\/p>\r\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\r\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p><strong>Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0<strong>Ways to connect PHP and MySQL<\/strong><\/p>\n<p>\u2022\u00a0<strong>Useful methods of PDO<\/strong><\/p>\n<p>\u2022\u00a0<strong>PHP and MySQL connectivity<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Web Database Architecture<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Web Application is a client\/server architecture. In typical web application client\/server architecture consist of two objects: 1. A web browser and 2. A web server. Web Browser and Web Server communicate via a communication link \u2013 generally internet or through http protocol. A web browser send request to web server. The Web server send response to the web server. This architecture is used for a server sends static web pages. which Following figure shows the simple client\/server architecture.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-239\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-111.png\" alt=\"\" width=\"762\" height=\"226\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-111.png 762w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-111-300x89.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-111-65x19.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-111-225x67.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-111-350x104.png 350w\" sizes=\"auto, (max-width: 762px) 100vw, 762px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The typical web database application consists of following stages.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1.Using browser, user send request to webserver using HTTP. For example, by entering city name, the user wants to get all the vendor details who belongs to city. By entering city name in HTML form and click submit button, he sends request to webserver. The search result page is fetch.php<\/p>\n<p style=\"text-align: justify\">2. The web server \u2013 Apache \u2013 receives the request and retrieve the file fetch.php. The webserver passes the file to PHP engine for processing.<\/p>\n<p style=\"text-align: justify\">3. The PHP engine starts parsing the script. Inside the script, there is a command to connect to the database and execute the query. For example, here the query is to search vendor details based on their city name. PHP opens a connection to MySQL server and send query to MySQL server.<\/p>\n<p style=\"text-align: justify\">4. MySQL server receives the query, process it and send the result back to the PHP engine<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">5. The PHP engine get the query result, finishes the script and formatting the query result in HTML. Then returns the resulting HTML to web server.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">6. The web server sends HTML back to the browser.<\/span><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">\u00a0 \u00a0The php engine and database server can run on the same machine or different machine.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Three ways to connect PHP and MySQL<\/strong><\/p>\n<ul>\n<li style=\"text-align: justify\">An Application Programming Interface (API) is used to communicate an application with other applications. MySQL provides an API which specifies how PHP can work with MySQL.<\/li>\n<li>PHP provides 3 extensions to work with MySQL API.<\/li>\n<\/ul>\n<ol>\n<li>MySQL extension<\/li>\n<li>MySQLi extension<\/li>\n<li>PDO MySQL driver extension<\/li>\n<\/ol>\n<ul>\n<li style=\"text-align: justify\">PDO (PHP Data Objects) is a database abstraction layer. It is used specifically for PHP applications. To communicate with MySQL, It uses PDO MySQL driver extension. PDO can use other drivers extension to communicate with other databases.<\/li>\n<li>Each extension has its own pros and cons.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>PDO (PHP Data Objects) Pros<\/strong><\/p>\n<ul>\n<li>Available for PHP 5.0 and later<\/li>\n<li>Object Oriented Interface<\/li>\n<li style=\"text-align: justify\">Consistent Interface which is portable between many database servers such as Oracle, DB2, MS SQL Server and Postgre SQL<\/li>\n<li>Use features available MySQL 4.1.3 and later<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 \u00a0PDO Cons<\/strong><\/p>\n<ul>\n<li>Does not work PHP 4.X or earlier<\/li>\n<li>Not use advances features of MySQL 4.1.3 and later such as multiple statements<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>MySQLi (MySQL improved extension) Pros<\/strong><\/p>\n<ul>\n<li>Included with PHP 5.X or later<\/li>\n<li>Provides Object-Oriented and Procedure Oriented Interface<\/li>\n<li>use all advances features of MySQL 4.1.3 and later<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 MySQLi Cons<\/strong><\/p>\n<\/div>\n<div>\n<ul>\n<li>Cannot used with other database servers<\/li>\n<\/ul>\n<p><strong>\u00a0 \u00a0 MySQL (MySQL extension) pros<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 Available for PHP 3.X, 4.X and 5.X<\/p>\n<p>&nbsp;<\/p>\n<p><strong>MySQL cons<\/strong><\/p>\n<ul>\n<li>Not use advances features of MySQL 4.1.3 and later such as multiple statements<\/li>\n<li>Not under active development<\/li>\n<\/ul>\n<p>PDO is used in this module for demonstration.<\/p>\n<ul>\n<li style=\"text-align: justify\">To use PDO object for making connection with MySQL, you must create a PDO object with three arguments. The three arguments are \u2013 DSN, User Name and Password.<\/li>\n<li>The general syntax is:<\/li>\n<\/ul>\n<p style=\"text-align: center\">new PDO($dsn, $username, $password)<\/p>\n<ul>\n<li style=\"text-align: justify\">DSN is short form of Data Source Name. The DSN specifies host name and database name. If MySQL database runs on the same server as PHP, then host name is localhost. The syntax of DSN for MySQL database is<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>mysql:host=hostaddress;dbname=databasename<\/p>\n<p>&nbsp;<\/p>\n<p>or you can set the values in a variable.<\/p>\n<p>&nbsp;<\/p>\n<p>$dsn = &#8216;mysql:host=localhost;dbname=account&#8217;<\/p>\n<ul>\n<li style=\"text-align: justify\">To call a method of the object, type object name, followed by object access operator \u2013 code as -&gt; &#8211; and followed by method name with arguments required for method in parentheses. Multiple arguments are separated by comma. The general syntax is<\/li>\n<\/ul>\n<p>objectName-&gt;methodName(Argument1 [, Argument2 ..])<\/p>\n<ul>\n<li style=\"text-align: justify\">To execute the select statement of SQL, query method of PDO object is used. The query method takes one parameter.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 95.0625px\"><strong>Method<\/strong><\/td>\n<td style=\"width: 533.063px\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 95.0625px\" rowspan=\"3\">query($select)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/td>\n<td style=\"width: 533.063px\" rowspan=\"3\">Execute the $select statement and returns a PDO statement object. The PDO<\/p>\n<p>statement object contains the result set of $select. If no result set is returned,<\/p>\n<p>then it returns false value.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<ul>\n<li>The general syntax to use query method is\u00a0 $PDOobject-&gt;query($select)<\/li>\n<li style=\"text-align: justify\">PDOStatement class has many methods. Below table describe some methods of the PDOStatement Class<\/li>\n<\/ul>\n<table class=\"aligncenter\" style=\"height: 114px\">\n<tbody>\n<tr style=\"height: 30px\">\n<td style=\"height: 30px;width: 53.0625px\"><strong>Method<\/strong><\/td>\n<td style=\"height: 30px;width: 502.063px\"><strong>Description<\/strong><\/td>\n<td style=\"height: 30px;width: 0.0625px\"><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 53.0625px\" rowspan=\"2\">fetch( )<\/p>\n<p>&nbsp;<\/td>\n<td style=\"height: 56px;width: 502.063px\" rowspan=\"2\">Returns an array having result set for next row. Array can be index by<\/p>\n<p>string and number. String is used for column name and number for column<\/td>\n<td style=\"height: 28px;width: 0.0625px\"><\/td>\n<\/tr>\n<tr style=\"height: 28px\">\n<td style=\"height: 28px;width: 0.0625px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td><\/td>\n<td>position. If no array is available , it returns false.<\/td>\n<\/tr>\n<tr>\n<td>fetchall( )<\/td>\n<td>Returns an array having all the rows of the result set.<\/td>\n<\/tr>\n<tr>\n<td>rowcount( )<\/td>\n<td>Returns the number of rows affected by the executing of last statement.<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">closeCursor( )<\/p>\n<p>&nbsp;<\/td>\n<td rowspan=\"2\">Close the cursor and frees the connection to the server so other SQL<\/p>\n<p>statement can be issued.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li style=\"text-align: justify\">The following code shows one example using the query and fetch method. There is a GUI which ask user to enter city name and click on submit button. The output is displayed all the details of vendor who lives in the city entered by user.<\/li>\n<li>The HTML code for GUI is shown below.<\/li>\n<\/ul>\n<p>&lt;html&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form action=&#8221;fetch.php&#8221; method=&#8221;post&#8221;&gt; Enter City Name:<\/p>\n<p>&lt;input type=&#8221;text&#8221; name=&#8221;city&#8221;&gt; &lt;br&gt;&lt;br&gt;<\/p>\n<p>&lt;input type=&#8221;submit&#8221; name=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt; &lt;input type=&#8221;reset&#8221; name=&#8221;reset&#8221; value=&#8221;Reset&#8221;&gt;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<ul>\n<li>The output of HTML code is shown below:<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-241 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-112.png\" alt=\"\" width=\"218\" height=\"112\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-112.png 218w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-112-65x33.png 65w\" sizes=\"auto, (max-width: 218px) 100vw, 218px\" \/><\/p>\n<ul>\n<li>The PHP Code for file fetch.php is shown below<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$dsn = &#8216;mysql:host=localhost;dbname=account&#8217;; $uname = &#8216;root&#8217;;<\/p>\n<p>$pwd = &#8221;;<\/p>\n<p>$db = new PDO($dsn,$uname,$pwd); $vcity = $_POST[&#8216;city&#8217;];<\/p>\n<p>$query = &#8220;select * from vendor<\/p>\n<p>where vcity = &#8216;$vcity'&#8221;;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Query : $query &lt;br&gt;&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$result = $db-&gt;query($query);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$row = $result-&gt;fetch();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">while($row != null)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$vid = $row[&#8216;vid&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$vname = $row[&#8216;vname&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$vaddress = $row[&#8216;vaddress&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$vcity = $row[&#8216;vcity&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$vmobile = $row[&#8216;vmobile&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt;&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Vendor ID : $vid&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Vendor Name: $vname&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Vendor Address: $vaddress&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Vendor City : $vcity&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Vendor Mobile : $vmobile&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$row = $result-&gt;fetch();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$result-&gt;closeCursor();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<ul>\n<li>The output is given below:<\/li>\n<li>Query : select * from vendor where vcity = &#8216;Rajkot&#8217;<\/li>\n<\/ul>\n<p style=\"text-align: left\">\u00a0 \u00a0 Vendor ID : 1<\/p>\n<p style=\"text-align: left\">Vendor Name: Anand<\/p>\n<p style=\"text-align: left\">Vendor Address: Ravi Park<\/p>\n<p style=\"text-align: left\">Vendor City : Rajkot<\/p>\n<p style=\"text-align: left\">Vendor Mobile : 9876034521<\/p>\n<p style=\"text-align: left\">Vendor ID : 2<\/p>\n<p style=\"text-align: left\">Vendor Name: Dharmesh<\/p>\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor Address: 19, Park Avenue<\/span><\/p>\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor City : Rajkot<\/span><\/p>\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor Mobile : 1234567890<\/span><\/p>\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor ID : 7<\/span><\/p>\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor Name: Manish<\/span><\/p>\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor Address: University Road<\/span><\/p>\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor City : Rajkot<\/span><\/p>\n<p style=\"text-align: left\"><span style=\"text-align: initial;font-size: 1em\">Vendor Mobile : 9087654321<\/span><\/p>\n<\/div>\n<div>\n<ul>\n<li style=\"text-align: justify\">In the above code, fetch() method is used. The same code be written using fetchall() method. Following code shows HTML and PHP code of the above program using fetchall() method.<\/li>\n<li>HTML code<\/li>\n<\/ul>\n<p>&lt;html&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form action=&#8221;fetchall.php&#8221; method=&#8221;post&#8221;&gt;<\/p>\n<p>Enter City Name:<\/p>\n<p>&lt;input type=&#8221;text&#8221; name=&#8221;city&#8221;&gt;<\/p>\n<p>&lt;br&gt;&lt;br&gt;<\/p>\n<p>&lt;input type=&#8221;submit&#8221; name=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt;<\/p>\n<p>&lt;input type=&#8221;reset&#8221; name=&#8221;reset&#8221; value=&#8221;Reset&#8221;&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<ul>\n<li>PHP Code<\/li>\n<\/ul>\n<p>&lt;?php<\/p>\n<p>$dsn = &#8216;mysql:host=localhost;dbname=account&#8217;; $uname = &#8216;root&#8217;;<\/p>\n<p>$pwd = &#8221;;<\/p>\n<p>$db = new PDO($dsn,$uname,$pwd); $vcity = $_POST[&#8216;city&#8217;];<\/p>\n<p>$query = &#8220;select *\u00a0<span style=\"font-size: 1em;text-align: initial\">from vendor\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">where vcity = &#8216;$vcity'&#8221;;<\/span><\/p>\n<\/div>\n<div>\n<p>\/\/echo &#8220;&lt;br&gt; Query : $query &lt;br&gt;&#8221;;<\/p>\n<p>$result = $db-&gt;query($query);<\/p>\n<p>$rows = $result-&gt;fetchall();<\/p>\n<p>foreach($rows as $row)<\/p>\n<p>{<\/p>\n<p>$vid = $row[&#8216;vid&#8217;];<\/p>\n<p>$vname = $row[&#8216;vname&#8217;];<\/p>\n<p>$vaddress = $row[&#8216;vaddress&#8217;];<\/p>\n<p>$vcity = $row[&#8216;vcity&#8217;];<\/p>\n<p>$vmobile = $row[&#8216;vmobile&#8217;];<\/p>\n<p>echo &#8220;&lt;br&gt;&#8221;;<\/p>\n<p>echo &#8220;&lt;br&gt; Vendor ID : $vid&#8221;;<\/p>\n<p>echo &#8220;&lt;br&gt; Vendor Name: $vname&#8221;;<\/p>\n<p>echo &#8220;&lt;br&gt; Vendor Address: $vaddress&#8221;;<\/p>\n<p>echo &#8220;&lt;br&gt; Vendor City : $vcity&#8221;;<\/p>\n<p>echo &#8220;&lt;br&gt; Vendor Mobile : $vmobile&#8221;;<\/p>\n<p>}<\/p>\n<p>$result-&gt;closeCursor();<\/p>\n<p>?&gt;<\/p>\n<ul>\n<li style=\"text-align: justify\">To execute insert, update or delete statement exec method of PDO object is used. Exec method takes insert, update or delete SQL statement as argument.<\/li>\n<\/ul>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td>Method<\/td>\n<td>Description<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">exec($SQLSelect)<\/p>\n<p>&nbsp;<\/td>\n<td rowspan=\"2\">Execute the $select and returns a number indicate how many rows are<\/p>\n<p>affected. If no rows are affected, then return zero.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>For example, to insert a new record in vendor table first create GUI. Following code shows<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">&lt;html&gt;<\/p>\n<p style=\"text-align: justify\">&lt;body&gt;<\/p>\n<p style=\"text-align: justify\">&lt;form action=&#8217;exec.php&#8217; method=&#8217;post&#8217;&gt; &lt;table&gt;<\/p>\n<p style=\"text-align: justify\">&lt;!&#8211; vid &#8211;&gt; &lt;tr&gt;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">&lt;td&gt;Vendor ID:&lt;\/td&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type=&#8217;text&#8217; name=&#8217;vid&#8217;&gt; &lt;\/td&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;!&#8211; vname &#8211;&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt;Vendor Name:&lt;\/td&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type=&#8217;text&#8217; name=&#8217;vname&#8217;&gt; &lt;\/td&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;!&#8211; vaddress\u00a0 &#8211;&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt;Vendor Address:&lt;\/td&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type=&#8217;text&#8217; name=&#8217;vaddress&#8217;&gt; &lt;\/td&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;!&#8211; vcity &#8211;&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; Vendor City: &lt;\/td&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type=&#8217;text&#8217; name=&#8217;vcity&#8217;&gt; &lt;\/td&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;!&#8211; vmobile &#8211;&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; Vendor Mobile: &lt;\/td&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type=&#8217;text&#8217; name=&#8217;vmobile&#8217;&gt; &lt;\/td&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;!&#8211; submit and reset &#8211;&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type=&#8217;submit&#8217; name=&#8217;submit&#8217; value=&#8217;Submit&#8217;&gt; &lt;\/td&gt; &lt;td&gt; &lt;input type=&#8217;reset&#8217; name=&#8217;reset&#8217; value=&#8217;Reset&#8217;&gt; &lt;\/td&gt; &lt;\/tr&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/table&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">\n<p>The output of the HTML code is :<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-242 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-113.png\" alt=\"\" width=\"270\" height=\"202\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-113.png 270w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-113-65x49.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-113-225x168.png 225w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/p>\n<\/div>\n<ul>\n<li><span style=\"text-align: initial;font-size: 1em\">The PHP code for file exec.php is given below.<\/span><\/li>\n<\/ul>\n<div>\n<p>\u00a0 \u00a0&lt;?php<\/p>\n<p>$dsn = &#8216;mysql:host=localhost;dbname=account&#8217;; $uname = &#8216;root&#8217;;<\/p>\n<p>$pwd = &#8221;;<\/p>\n<p>$db = new PDO($dsn,$uname,$pwd);<\/p>\n<p>$vid = $_POST[&#8216;vid&#8217;];<\/p>\n<p>$vname = $_POST[&#8216;vname&#8217;];<\/p>\n<p>$vaddress = $_POST[&#8216;vaddress&#8217;]; $vcity = $_POST[&#8216;vcity&#8217;]; $vmobile = $_POST[&#8216;vmobile&#8217;];<\/p>\n<p>$query = &#8220;insert into vendor<\/p>\n<p>values($vid,&#8217;$vname&#8217;,&#8217;$vaddress&#8217;,&#8217;$vcity&#8217;,&#8217;$vmobile&#8217;) &#8220;;<\/p>\n<p>echo &#8220;&lt;br&gt; Query : $query &lt;br&gt;&#8221;; $result = $db-&gt;exec($query);<\/p>\n<p>echo &#8220;&lt;br&gt; Result is : $result&#8221;; if($result &gt; 0)<\/p>\n<p>{<\/p>\n<p>echo &#8220;&lt;br&gt; Record Insrted Successfully&#8221;;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<ul>\n<li>The output is<\/li>\n<\/ul>\n<\/div>\n<div>\n<p style=\"text-align: center\">\u00a0 \u00a0Query : insert into vendor values(22,&#8217;Kalpesh&#8217;,&#8217;Bopal&#8217;,&#8217;Ahmedabad&#8217;,&#8217;9753124680&#8242;)<\/p>\n<p style=\"text-align: center\">Result is : 1<\/p>\n<p style=\"text-align: center\">Record Insrted Successfully<\/p>\n<ul>\n<li style=\"text-align: justify\">For live application development, prepare method is used. Prepare method is a method of PDO object. Prepare statement improves database performance and security.<\/li>\n<li style=\"text-align: justify\">When a statement is executed multiple times, the statements allow the database server to reuse of the work which is done when prepare a statement.<\/li>\n<li style=\"text-align: justify\">Prepare statement also improves security because it can prevent most types of the SQL injection attacks.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-243 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-114.png\" alt=\"\" width=\"682\" height=\"272\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-114.png 682w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-114-300x120.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-114-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-114-225x90.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-114-350x140.png 350w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/p>\n<p>Let\u2019s take one example using prepare statement. The HTML code is shown below<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form action=&#8217;prepareselect.php&#8217; method=&#8217;post&#8217;&gt; Enter city name :<\/p>\n<p>&lt;input type=&#8221;text&#8221; name=&#8221;vcity&#8221;&gt; &lt;\/input&gt;<\/p>\n<p>&lt;br&gt;&lt;br&gt;<\/p>\n<p>&lt;input type=&#8217;submit&#8217; name=&#8217;submit&#8217; value=&#8217;Submit&#8217;&gt; &lt;input type=&#8217;reset&#8217; name=&#8217;reset&#8217; value=&#8217;Reset&#8217;&gt; &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-244 aligncenter\" style=\"text-align: initial;font-size: 1em\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-115.png\" alt=\"\" width=\"236\" height=\"134\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-115.png 236w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-115-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-115-225x128.png 225w\" sizes=\"auto, (max-width: 236px) 100vw, 236px\" \/><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0The PHP code for the file prepareselect.php is shown below.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$dsn=&#8217;mysql:host=localhost;dbname=account&#8217;;<\/p>\n<p>$uname = &#8216;root&#8217;;<\/p>\n<p>$pwd = &#8221;;<\/p>\n<p>$db = new PDO($dsn,$uname,$pwd); $vcity = $_POST[&#8216;vcity&#8217;];<\/p>\n<p>$query = &#8220;select * from vendor<\/p>\n<p>where vcity = :vcity&#8221;;<\/p>\n<p>$result = $db-&gt;prepare($query);<\/p>\n<p>$result-&gt;bindValue(&#8216;:vcity&#8217;,$vcity); $result-&gt;execute();<\/p>\n<p>$rows = $result-&gt;fetchAll();<\/p>\n<p>foreach($rows as $r)<\/p>\n<p>{<\/p>\n<p>echo &#8216;&lt;br&gt; Vendor ID :&#8217;. $r[&#8216;vid&#8217;];<\/p>\n<p>echo &#8216;&lt;br&gt; Vendor Name :&#8217;. $r[&#8216;vname&#8217;];<\/p>\n<p>echo &#8216;&lt;br&gt; Vendor Address :&#8217;. $r[&#8216;vaddress&#8217;];<\/p>\n<p>echo &#8216;&lt;br&gt; Vendor City :&#8217;. $r[&#8216;vcity&#8217;];<\/p>\n<p>echo &#8216;&lt;br&gt; Vendor Mobile :&#8217;. $r[&#8216;vmobile&#8217;];<\/p>\n<p>echo &#8220;&lt;br&gt;&#8221;;<\/p>\n<p>}<\/p>\n<p>$result-&gt;closeCursor();<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>As the user clicks on submit button, he\/she will get the output as shown below.<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 Vendor ID :1<\/p>\n<p>Vendor Name :Anand<\/p>\n<p>Vendor Address :Ravi Park<\/p>\n<p>Vendor City :Rajkot<\/p>\n<p>Vendor Mobile :9876034521<\/p>\n<p>&nbsp;<\/p>\n<p>Vendor ID :2<\/p>\n<p>Vendor Name :Dharmesh<\/p>\n<p>Vendor Address :19, Park Avenue<\/p>\n<p>Vendor City :Rajkot<\/p>\n<p>Vendor Mobile :1234567890<\/p>\n<p>&nbsp;<\/p>\n<p>Vendor ID :7<\/p>\n<p>Vendor Name :Manish<\/p>\n<p>Vendor Address :University Road<\/p>\n<p>Vendor City :Rajkot<\/p>\n<p>Vendor Mobile :9087654321<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the similar way, prepare and bindValue method can be used to insert,update or delete statement. Following code shows to insert a record using prpare and bindValue.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form action=&#8217;prepareinsert.php&#8217; method=&#8217;post&#8217;&gt;<\/p>\n<p>&lt;table&gt;<\/p>\n<p>&lt;!&#8211; vid &#8211;&gt; &lt;tr&gt;<\/p>\n<p>&lt;td&gt;Vendor ID:&lt;\/td&gt;<\/p>\n<p>&lt;td&gt; &lt;input type=&#8217;text&#8217; name=&#8217;vid&#8217;&gt; &lt;\/td&gt; &lt;\/tr&gt;<\/p>\n<p>&lt;!&#8211; vname &#8211;&gt; &lt;tr&gt;<\/p>\n<p>&lt;td&gt;Vendor Name:&lt;\/td&gt;<\/p>\n<p>&lt;td&gt; &lt;input type=&#8217;text&#8217; name=&#8217;vname&#8217;&gt; &lt;\/td&gt; &lt;\/tr&gt;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">&lt;!&#8211; vaddress\u00a0 &#8211;&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt;Vendor Address:&lt;\/td&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type=&#8217;text&#8217; name=&#8217;vaddress&#8217;&gt; &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;!&#8211; vcity &#8211;&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; Vendor City: &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type=&#8217;text&#8217; name=&#8217;vcity&#8217;&gt; &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;!&#8211; vmobile &#8211;&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; Vendor Mobile: &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type=&#8217;text&#8217; name=&#8217;vmobile&#8217;&gt; &lt;\/td&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/tr&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;!&#8211; submit and reset &#8211;&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;tr&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;td&gt; &lt;input type=&#8217;submit&#8217; name=&#8217;submit&#8217; value=&#8217;Submit&#8217;&gt; &lt;\/td&gt; &lt;td&gt; &lt;input type=&#8217;reset&#8217; name=&#8217;reset&#8217; value=&#8217;Reset&#8217;&gt; &lt;\/td&gt; &lt;\/tr&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/table&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span><\/p>\n<\/div>\n<div><\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-245 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-116.png\" alt=\"\" width=\"240\" height=\"212\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-116.png 240w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-116-65x57.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-116-225x199.png 225w\" sizes=\"auto, (max-width: 240px) 100vw, 240px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The code for prepareinsert.php is shown below.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0&lt;?php<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$dsn=&#8217;mysql:host=localhost;dbname=account&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$uname = &#8216;root&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$pwd = &#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$db = new PDO($dsn,$uname,$pwd);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$vid = $_POST[&#8216;vid&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$vname = $_POST[&#8216;vname&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$vaddress = $_POST[&#8216;vaddress&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$vcity = $_POST[&#8216;vcity&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$vmobile = $_POST[&#8216;vmobile&#8217;];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$query = &#8220;insert into vendor values (:vid, :vname, :vaddress, :vcity, :vmobile)&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$result = $db-&gt;prepare($query);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$result-&gt;bindValue(&#8216;:vid&#8217;,$vid);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$result-&gt;bindValue(&#8216;:vname&#8217;,$vname);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$result-&gt;bindValue(&#8216;:vaddress&#8217;,$vaddress);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$result-&gt;bindValue(&#8216;:vcity&#8217;,$vcity);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$result-&gt;bindValue(&#8216;:vmobile&#8217;,$vmobile);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$success = $result-&gt;execute();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if($success &gt; 0)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; Record Inserted Successfully &#8220;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$result-&gt;closeCursor();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0Output for the program is<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"font-family: 'Cormorant Garamond', serif;font-size: 1.66667em\">Record Inserted Successfully<\/strong><\/p>\n<\/div>\n<div>\n<ul>\n<li style=\"text-align: justify\">An exception is an object which contains information about error which has occurred. Some PHP statements throw exception when an error is occurred. If an exception is not handled, then the applications terminate with error. To handle exception, try catch statement is used.<\/li>\n<li>To handle the errors thrown by the PDO library, the PDO Exception class is used.<\/li>\n<li>The below code shows an example of PDO Exception.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 &lt;?php<\/p>\n<p>\/\/ PDO Database Object created<\/p>\n<p>$dsn =&#8217;mysql:host=localhost;dbname=account&#8217;;<\/p>\n<p>$uname = &#8216;root&#8217;;<\/p>\n<p>$pwd = &#8216;pwd&#8217;;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>$db = new PDO($dsn,$uname,$pwd);<\/p>\n<p>echo &#8220;&lt;br&gt; Connection established successfully&#8221;;<\/p>\n<p>\/\/<\/p>\n<p>$query = &#8220;select * from vendor&#8221;;<\/p>\n<p>echo &#8216;&lt;br&gt;&#8217;. $query . &#8216;&lt;br&gt;&#8217;;<\/p>\n<p>$rows = $db-&gt;query($query);<\/p>\n<p>$row = $rows-&gt;fetch();<\/p>\n<p>while ($row != null)<\/p>\n<p>{<\/p>\n<p>$vid = $row[&#8216;vid&#8217;];<\/p>\n<p>$vname = $row[&#8216;vname&#8217;];<\/p>\n<p>$vaddress = $row[&#8216;vaddress&#8217;];<\/p>\n<p>$vcity = $row[&#8216;vcity&#8217;];<\/p>\n<p>$vmobile = $row[&#8216;vmobile&#8217;];<\/p>\n<p>echo &#8216;&lt;br&gt;&#8217;;<\/p>\n<p>echo &#8220;vid : $vid &lt;br&gt;&#8221;;<\/p>\n<p>echo &#8220;vname : $vname &lt;br&gt;&#8221;;<\/p>\n<p>echo &#8220;vaddress : $vaddress &lt;br&gt; &#8220;;<\/p>\n<p>echo &#8220;vcity : $vcity &lt;br&gt; &#8220;;<\/p>\n<p>echo &#8220;vmobile : $vmobile &lt;br&gt; &#8220;;<\/p>\n<p>\/\/ fetch next record<\/p>\n<p>$row = $rows-&gt;fetch();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>catch(PDOException $e)<\/p>\n<p>{<\/p>\n<p>$errormessage = $e-&gt;getMessage();<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8220;&lt;br&gt; An error occured when connecting with database. The error is :<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$errormessage&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<ul>\n<li>The output of the program is<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">An error occured when connecting with database. The error is : SQLSTATE[28000]<\/p>\n<p>[1045] Access denied for user &#8216;root&#8217;@&#8217;localhost&#8217; (using password: YES)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>References:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional, Apress<\/p>\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass:Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O&#8217;Reilly Media<\/p>\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove: Professional PHP,Wrox<\/p>\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide, Pearson Education<\/p>\n<p style=\"text-align: justify\">11. http:\/\/www.php.net\/<\/p>\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\n","protected":false},"author":3,"menu_order":25,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-hiren-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-237","chapter","type-chapter","status-publish","hentry","contributor-dr-hiren-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/237\/revisions"}],"predecessor-version":[{"id":425,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/237\/revisions\/425"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/237\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=237"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=237"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=237"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}