{"id":285,"date":"2018-07-13T07:27:28","date_gmt":"2018-07-13T07:27:28","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=285"},"modified":"2018-12-07T11:50:22","modified_gmt":"2018-12-07T11:50:22","slug":"integrating-php-and-mysql","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/integrating-php-and-mysql\/","title":{"rendered":"Integrating PHP and MySQL"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/yeiMgOpnBHM\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>PHP<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">PHP (recursive acronym* ,Hypertext Preprocessor) is an open source scripting language which is used for web development and can be embedded into HTML. It is widely used open source language for developing web applications. Any text editor like notepad or notepad++ can be used to write the scripts for PHP. Various Frameworks are also available for managing PHP code, which we will cover at a later stage.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">*PHP- Recursive acronym (Hypertext Preprocessor ) (Originally \u201cPersonal home page tools\u201d)<\/p>\r\nXAMP \u2013 XAMP APACHE MARIADB PHP PERl\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Note <\/strong>: There are other variants like Lamp for Linux , Wamp for Windows , MAMP for MAC.We will be using <strong>XAMP<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Before you start running your PHP scripts, you need to start some of the Services like Apache , MySQL etc.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-287 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-138.png\" alt=\"\" width=\"672\" height=\"376\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>XAMP<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">You can also edit the XAMPP setting from my.ini , which is as follows<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-289 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-139.png\" alt=\"\" width=\"651\" height=\"426\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nBut , if you have MySQL workbench installed , services might not start, the way they should.\r\n\r\n&nbsp;\r\n\r\nTroubleshooting Services :\r\n\r\n&nbsp;\r\n\r\n<strong>XAMP &amp; MySQL<\/strong>\r\n\r\n&nbsp;\r\n\r\nYou can edit the settings in my.ini as follows: We have changed the port number to 3307\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-290 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-140.png\" alt=\"\" width=\"612\" height=\"375\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Services (services.msc)<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-291 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-141.png\" alt=\"\" width=\"625\" height=\"427\" \/>\r\n\r\n<strong>PHPMyadmin<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">PhpMyAdmin is Open Source administration tool for MySQL and MariaDB. It is written in PHP and supports wide range of operations like managing tables , columns , indexes , user<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-292 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-142.png\" alt=\"\" width=\"630\" height=\"327\" \/>\r\n<div>\r\n<p style=\"text-align: left\">\u00a0 \u00a0 \u00a0 permissions etc.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: left\"><strong>Data objects in PHP<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: left\">\u2022 There are different ways of accessing Database in PHP<\/p>\r\n<p style=\"text-align: left\">\u2022 PDO : Lightweight consistent interface for accessing databases in PHP<\/p>\r\n<p style=\"text-align: left\">\u2022 SDO : Sequence Data objects (for unstructured data)<\/p>\r\n<p style=\"text-align: left\">\u2022 Various functions for manipulating Databases like , mysql_connect , mysqli_connect etc.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-293 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-143.png\" alt=\"\" width=\"647\" height=\"414\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>DB connectivity testing<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$dbname = 'test';\r\n\r\n$dbuser = 'root';\r\n\r\n$dbpass = 'Your Password';\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$dbhost = 'localhost';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(\"Unable to Connect to '$dbhost'\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$connect = mysqli_connect ($dbhost, $dbuser, $dbpass) or die (\"could not connect to '$dbhost'\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if (!$connect)<\/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\">die (\"connection failed \". mysqli_connect_error());<\/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 \"connected successfully\";<\/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<p style=\"text-align: justify\">Please note that the commented lines of mysql_connect might not be supported by some data editors now. Hence , we have used , mysqli_connect.<\/p>\r\n&nbsp;\r\n\r\nYou can test the above script by running into your browser.\r\n\r\nNow , let us start with calling the stored procedures, which we created in MySQL into our PHP script\r\n\r\n&nbsp;\r\n\r\n<strong>Stored Procedure:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 A stored procedure or a function is a named Pl\/SQL block which resides in the Database engine\u2019s tables. A stored procedure can be invoked by other procedure, triggers or by other applications like PHP, java etc.<\/p>\r\n<p style=\"text-align: justify\">\u2022 It is a logically grouped set of SQL and Pl\/SQL statements which are written to perform a specific task.<\/p>\r\n<p style=\"text-align: justify\">\u2022 Procedures and functions are also referred to as Database objects and such objects can be invoked or called by any Pl\/SQL block within the application.<\/p>\r\n&nbsp;\r\n\r\n<strong>Calling Stored procedure in PHP<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt; GEt salary &lt;\/title&gt;\r\n\r\n&lt;link rel=\"stylesheet\" href=\"css\/table.css\" type=\"text\/css\" \/&gt;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">&lt;\/head&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;?php<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/require_once 'dbconfig.php';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$host = 'localhost';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$dbname = 'test';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$username = 'root';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$password = 'Your Password';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">try {<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$pdo = new PDO(\"mysql:host=$host;dbname=$dbname\", $username, $password);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 execute the stored procedure $sql = 'CALL Get_sal()';<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 call the stored procedure<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 $q = $pdo\u00a0\u00a0\u00a0 query($sql);\r\n\r\n$q-&gt;setFetchMode(PDO::FETCH_ASSOC);\r\n\r\n} catch (PDOException $e) {\r\n\r\ndie(\"Error occurred:\" . $e-&gt;getMessage());\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n&lt;table&gt;\r\n\r\n&lt;tr&gt;\r\n\r\n&lt;th&gt;Employee Name&lt;\/th&gt;\r\n\r\n&lt;th&gt;Salary&lt;\/th&gt;\r\n\r\n&lt;\/tr&gt;\r\n\r\n&lt;?php while ($r = $q-&gt;fetch()): ?&gt;\r\n\r\n&lt;tr&gt;\r\n\r\n&lt;td&gt;&lt;?php echo $r['name'] ?&gt;&lt;\/td&gt;\r\n\r\n&lt;td&gt;&lt;?php echo '\u20b9' . number_format($r['salary'], 2) ?&gt; &lt;\/td&gt;\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0&lt;\/tr&gt;\r\n\r\n&lt;?php endwhile; ?&gt;\r\n\r\n&lt;\/table&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n&nbsp;\r\n\r\nNote: To insert Rupee symbol, you can use ALT+8377 or ALT+4 on your keyboard\r\n\r\nAfter running your program in the browser, you get the following output:\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-294 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-144.png\" alt=\"\" width=\"524\" height=\"252\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the previous example ,we had used PDO and hence here we have used mysql_iconnect, so you can have clarity for both and use whichever you are comfortable with.<\/p>\r\n&nbsp;\r\n\r\n<strong>Triggers<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2022 Database Triggers are the database objects which reside in system catalog. The triggers are special type of procedures which can be called implicitly.<\/p>\r\n<p style=\"text-align: justify\">\u2022 Each trigger is associated with a table which can be activated on any DML statement like (Insert , update or delete).<\/p>\r\n<p style=\"text-align: justify\">To view the effect of Triggers created , you need to issue any DML statements in order to see the corresponding effect.<\/p>\r\n&nbsp;\r\n\r\nSo, let us have a program where we fire Update statement and see the Triggers effect.\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">UpdateSalary.PHP<\/strong>\r\n\r\n&nbsp;\r\n<div>\r\n\r\n\u00a0 \u00a0 &lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt;<strong>Trigger effect with Update EMP<\/strong>&lt;\/title&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;?php\r\n\r\nif(isset($_POST['update'])) {\r\n\r\n$host = 'localhost';\r\n\r\n$dbname = 'test';\r\n\r\n$username = 'root';\r\n\r\n$password = 'Your Password';\r\n\r\n$conn = mysqli_connect($host, $username, $password, $dbname); if(! $conn ) {\r\n\r\ndie('Could not connect: ' . mysqli_error($conn));\r\n\r\n}\r\n\r\n$empid = $_POST['empid'];\r\n\r\n$salary = $_POST['salary'];\r\n\r\n$sql = \"UPDATE emp \". \"SET salary = $salary \".\r\n\r\n\"WHERE empid = $empid\" ;\r\n\r\n$retval = mysqli_query($conn, $sql);\r\n\r\nif(! $retval ) {\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">die('Could not update data: ' . mysqli_error($conn));<\/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 \"Updated data successfully\\n\";<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">mysqli_close($conn);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}else {<\/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\r\n\u00a0 \u00a0&lt;form method = \"post\" action = \"&lt;?php $_PHP_SELF ?&gt;\"&gt; &lt;table width = \"400\" border =\" 0\" cellspacing = \"1\"\r\n\r\ncellpadding = \"2\"&gt;\r\n\r\n&lt;tr&gt;\r\n\r\n&lt;td width = \"100\"&gt;Employee ID&lt;\/td&gt;\r\n\r\n&lt;td&gt;&lt;input name = \"empid\" type = \"text\"\r\n\r\nid = \"empid\"&gt;&lt;\/td&gt;\r\n\r\n&lt;\/tr&gt;\r\n\r\n&lt;tr&gt;\r\n\r\n&lt;td width = \"100\"&gt;Employee Salary&lt;\/td&gt;\r\n\r\n&lt;td&gt;&lt;input name = \"salary\" type = \"text\"\r\n\r\nid = \"salary\"&gt;&lt;\/td&gt;\r\n\r\n&lt;\/tr&gt;\r\n\r\n&lt;tr&gt;\r\n\r\n&lt;td width = \"100\"&gt; &lt;\/td&gt;\r\n\r\n&lt;td&gt; &lt;\/td&gt;\r\n\r\n&lt;\/tr&gt;\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0&lt;tr&gt;\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 &lt;td width = \"100\"&gt; &lt;\/td&gt;\r\n\r\n&lt;td&gt;\r\n\r\n&lt;input name = \"update\" type = \"submit\"\r\n\r\nid = \"update\" value = \"Update\"&gt;\r\n\r\n&lt;\/td&gt;\r\n\r\n&lt;\/tr&gt;\r\n\r\n&lt;\/table&gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&lt;?php\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n&nbsp;\r\n\r\nAfter writing the program , execute in the browser , accept values from the user.\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-295 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-145.png\" alt=\"\" width=\"546\" height=\"297\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here, we are accepting the amount and id to be updated and then you can go and check into the tables the trigger effects.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nWe saw the above program with Mysqli_connect.\r\n\r\n&nbsp;\r\n\r\nNow , let us also have an example with PDO\r\n\r\n&nbsp;\r\n\r\n<strong>UpdateSalary_pdo.PHP<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt;Trigger effect with Update EMp PDO&lt;\/title&gt; &lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form method = \"post\" action = \"&lt;?php $_PHP_SELF ?&gt;\"&gt; &lt;table width = \"400\" border =\" 0\" cellspacing = \"1\"\r\n\r\ncellpadding = \"2\"&gt;\r\n\r\n&lt;tr&gt;\r\n\r\n&lt;td width = \"100\"&gt;Employee ID&lt;\/td&gt;\r\n\r\n&lt;td&gt;&lt;input name = \"empid\" type = \"text\"\r\n\r\nid = \"empid\" value=\"\"&gt;&lt;\/td&gt;\r\n\r\n&lt;\/tr&gt;\r\n\r\n&lt;tr&gt;\r\n\r\n&lt;td width = \"100\"&gt;Employee Salary&lt;\/td&gt;\r\n\r\n&lt;td&gt;&lt;input name = \"salary\" type = \"text\"\r\n\r\nid = \"salary\" value=\"\"&gt;&lt;\/td&gt;\r\n\r\n&lt;\/tr&gt;\r\n\r\n&lt;tr&gt;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;td width = \"100\"&gt; &lt;\/td&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 &lt;td&gt; &lt;\/td&gt;\r\n\r\n&lt;\/tr&gt;\r\n\r\n&lt;tr&gt;\r\n\r\n&lt;td width = \"100\"&gt; &lt;\/td&gt;\r\n\r\n&lt;td&gt;\r\n\r\n&lt;input name = \"update\" type = \"submit\"\r\n\r\nid = \"update\" value = \"Update\"&gt;\r\n\r\n&lt;\/td&gt;\r\n\r\n&lt;\/tr&gt;\r\n\r\n&lt;\/table&gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n$host = 'localhost';\r\n\r\n$dbname = 'test';\r\n\r\n$username = 'root';\r\n\r\n$password = '';\r\n\r\nif (isset ($_POST[\"update\"])){\r\n\r\n$empid = $_POST['empid'];\r\n\r\n$salary = $_POST['salary'];\r\n\r\ntry {\r\n\r\n$connection = new\r\n\r\nPDO(\"mysql:host=$host;dbname=$dbname\",$username,$password);\r\n\r\n$connection-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\r\n\r\n$sql = \"UPDATE emp SET salary=:salary WHERE empid=:empid\";\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$statement = $connection-&gt;prepare($sql);<\/span>\r\n\r\n<\/div>\r\n$statement-&gt;bindValue(\":empid\", $empid);\r\n\r\n$statement-&gt;bindValue(\":salary\", $salary);\r\n\r\n$count = $statement-&gt;execute();\r\n\r\n$connection = null;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Disconnect\r\n\r\n}\r\n\r\ncatch(PDOException $e) {\r\n\r\necho $e-&gt;getMessage();\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n&nbsp;\r\n\r\nYou can run the above program in the browser and notice the effects.\r\n\r\nYou can also add messages like Number of rows affected etc.\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Integrating PHP and MySQL<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/yeiMgOpnBHM\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<strong>Additional Reading:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1) MySQL 5 for professionals , Ivan Bayross , Sharanam Shah, Shroff Publishers<\/p>\r\n<p style=\"text-align: justify\">2) MySQL documentation : dev.mysql.com\/doc\/<\/p>\r\n<p style=\"text-align: justify\">3) PHP and MySQL Web Development, \u201cLuke Welling, Laura Thomson\u201d,Pearson Publications.<\/p>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/yeiMgOpnBHM\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>PHP<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">PHP (recursive acronym* ,Hypertext Preprocessor) is an open source scripting language which is used for web development and can be embedded into HTML. It is widely used open source language for developing web applications. Any text editor like notepad or notepad++ can be used to write the scripts for PHP. Various Frameworks are also available for managing PHP code, which we will cover at a later stage.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">*PHP- Recursive acronym (Hypertext Preprocessor ) (Originally \u201cPersonal home page tools\u201d)<\/p>\n<p>XAMP \u2013 XAMP APACHE MARIADB PHP PERl<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Note <\/strong>: There are other variants like Lamp for Linux , Wamp for Windows , MAMP for MAC.We will be using <strong>XAMP<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Before you start running your PHP scripts, you need to start some of the Services like Apache , MySQL etc.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-287 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-138.png\" alt=\"\" width=\"672\" height=\"376\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-138.png 672w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-138-300x168.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-138-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-138-225x126.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-138-350x196.png 350w\" sizes=\"auto, (max-width: 672px) 100vw, 672px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>XAMP<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">You can also edit the XAMPP setting from my.ini , which is as follows<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-289 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-139.png\" alt=\"\" width=\"651\" height=\"426\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-139.png 651w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-139-300x196.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-139-65x43.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-139-225x147.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-139-350x229.png 350w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>But , if you have MySQL workbench installed , services might not start, the way they should.<\/p>\n<p>&nbsp;<\/p>\n<p>Troubleshooting Services :<\/p>\n<p>&nbsp;<\/p>\n<p><strong>XAMP &amp; MySQL<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>You can edit the settings in my.ini as follows: We have changed the port number to 3307<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-290 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-140.png\" alt=\"\" width=\"612\" height=\"375\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-140.png 612w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-140-300x184.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-140-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-140-225x138.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-140-350x214.png 350w\" sizes=\"auto, (max-width: 612px) 100vw, 612px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Services (services.msc)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-291 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-141.png\" alt=\"\" width=\"625\" height=\"427\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-141.png 625w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-141-300x205.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-141-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-141-225x154.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-141-350x239.png 350w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><strong>PHPMyadmin<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">PhpMyAdmin is Open Source administration tool for MySQL and MariaDB. It is written in PHP and supports wide range of operations like managing tables , columns , indexes , user<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-292 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-142.png\" alt=\"\" width=\"630\" height=\"327\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-142.png 630w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-142-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-142-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-142-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-142-350x182.png 350w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/p>\n<div>\n<p style=\"text-align: left\">\u00a0 \u00a0 \u00a0 permissions etc.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: left\"><strong>Data objects in PHP<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: left\">\u2022 There are different ways of accessing Database in PHP<\/p>\n<p style=\"text-align: left\">\u2022 PDO : Lightweight consistent interface for accessing databases in PHP<\/p>\n<p style=\"text-align: left\">\u2022 SDO : Sequence Data objects (for unstructured data)<\/p>\n<p style=\"text-align: left\">\u2022 Various functions for manipulating Databases like , mysql_connect , mysqli_connect etc.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-293 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-143.png\" alt=\"\" width=\"647\" height=\"414\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-143.png 647w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-143-300x192.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-143-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-143-225x144.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-143-350x224.png 350w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>DB connectivity testing<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$dbname = &#8216;test&#8217;;<\/p>\n<p>$dbuser = &#8216;root&#8217;;<\/p>\n<p>$dbpass = &#8216;Your Password&#8217;;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$dbhost = &#8216;localhost&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(&#8220;Unable to Connect to &#8216;$dbhost'&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$connect = mysqli_connect ($dbhost, $dbuser, $dbpass) or die (&#8220;could not connect to &#8216;$dbhost'&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if (!$connect)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">die (&#8220;connection failed &#8220;. mysqli_connect_error());<\/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;connected successfully&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">Please note that the commented lines of mysql_connect might not be supported by some data editors now. Hence , we have used , mysqli_connect.<\/p>\n<p>&nbsp;<\/p>\n<p>You can test the above script by running into your browser.<\/p>\n<p>Now , let us start with calling the stored procedures, which we created in MySQL into our PHP script<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Stored Procedure:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 A stored procedure or a function is a named Pl\/SQL block which resides in the Database engine\u2019s tables. A stored procedure can be invoked by other procedure, triggers or by other applications like PHP, java etc.<\/p>\n<p style=\"text-align: justify\">\u2022 It is a logically grouped set of SQL and Pl\/SQL statements which are written to perform a specific task.<\/p>\n<p style=\"text-align: justify\">\u2022 Procedures and functions are also referred to as Database objects and such objects can be invoked or called by any Pl\/SQL block within the application.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Calling Stored procedure in PHP<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;title&gt; GEt salary &lt;\/title&gt;<\/p>\n<p>&lt;link rel=&#8221;stylesheet&#8221; href=&#8221;css\/table.css&#8221; type=&#8221;text\/css&#8221; \/&gt;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">&lt;\/head&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;?php<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/require_once &#8216;dbconfig.php&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$host = &#8216;localhost&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$dbname = &#8216;test&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$username = &#8216;root&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$password = &#8216;Your Password&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">try {<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$pdo = new PDO(&#8220;mysql:host=$host;dbname=$dbname&#8221;, $username, $password);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 execute the stored procedure $sql = &#8216;CALL Get_sal()&#8217;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0 call the stored procedure<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 $q = $pdo\u00a0\u00a0\u00a0 query($sql);<\/p>\n<p>$q-&gt;setFetchMode(PDO::FETCH_ASSOC);<\/p>\n<p>} catch (PDOException $e) {<\/p>\n<p>die(&#8220;Error occurred:&#8221; . $e-&gt;getMessage());<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>&lt;table&gt;<\/p>\n<p>&lt;tr&gt;<\/p>\n<p>&lt;th&gt;Employee Name&lt;\/th&gt;<\/p>\n<p>&lt;th&gt;Salary&lt;\/th&gt;<\/p>\n<p>&lt;\/tr&gt;<\/p>\n<p>&lt;?php while ($r = $q-&gt;fetch()): ?&gt;<\/p>\n<p>&lt;tr&gt;<\/p>\n<p>&lt;td&gt;&lt;?php echo $r[&#8216;name&#8217;] ?&gt;&lt;\/td&gt;<\/p>\n<p>&lt;td&gt;&lt;?php echo &#8216;\u20b9&#8217; . number_format($r[&#8216;salary&#8217;], 2) ?&gt; &lt;\/td&gt;<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0&lt;\/tr&gt;<\/p>\n<p>&lt;?php endwhile; ?&gt;<\/p>\n<p>&lt;\/table&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Note: To insert Rupee symbol, you can use ALT+8377 or ALT+4 on your keyboard<\/p>\n<p>After running your program in the browser, you get the following output:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-294 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-144.png\" alt=\"\" width=\"524\" height=\"252\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-144.png 524w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-144-300x144.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-144-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-144-225x108.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-144-350x168.png 350w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the previous example ,we had used PDO and hence here we have used mysql_iconnect, so you can have clarity for both and use whichever you are comfortable with.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Triggers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2022 Database Triggers are the database objects which reside in system catalog. The triggers are special type of procedures which can be called implicitly.<\/p>\n<p style=\"text-align: justify\">\u2022 Each trigger is associated with a table which can be activated on any DML statement like (Insert , update or delete).<\/p>\n<p style=\"text-align: justify\">To view the effect of Triggers created , you need to issue any DML statements in order to see the corresponding effect.<\/p>\n<p>&nbsp;<\/p>\n<p>So, let us have a program where we fire Update statement and see the Triggers effect.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">UpdateSalary.PHP<\/strong><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p>\u00a0 \u00a0 &lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;title&gt;<strong>Trigger effect with Update EMP<\/strong>&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;?php<\/p>\n<p>if(isset($_POST[&#8216;update&#8217;])) {<\/p>\n<p>$host = &#8216;localhost&#8217;;<\/p>\n<p>$dbname = &#8216;test&#8217;;<\/p>\n<p>$username = &#8216;root&#8217;;<\/p>\n<p>$password = &#8216;Your Password&#8217;;<\/p>\n<p>$conn = mysqli_connect($host, $username, $password, $dbname); if(! $conn ) {<\/p>\n<p>die(&#8216;Could not connect: &#8216; . mysqli_error($conn));<\/p>\n<p>}<\/p>\n<p>$empid = $_POST[&#8217;empid&#8217;];<\/p>\n<p>$salary = $_POST[&#8216;salary&#8217;];<\/p>\n<p>$sql = &#8220;UPDATE emp &#8220;. &#8220;SET salary = $salary &#8220;.<\/p>\n<p>&#8220;WHERE empid = $empid&#8221; ;<\/p>\n<p>$retval = mysqli_query($conn, $sql);<\/p>\n<p>if(! $retval ) {<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">die(&#8216;Could not update data: &#8216; . mysqli_error($conn));<\/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;Updated data successfully\\n&#8221;;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">mysqli_close($conn);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}else {<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">?&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0&lt;form method = &#8220;post&#8221; action = &#8220;&lt;?php $_PHP_SELF ?&gt;&#8221;&gt; &lt;table width = &#8220;400&#8221; border =&#8221; 0&#8243; cellspacing = &#8220;1&#8221;<\/p>\n<p>cellpadding = &#8220;2&#8221;&gt;<\/p>\n<p>&lt;tr&gt;<\/p>\n<p>&lt;td width = &#8220;100&#8221;&gt;Employee ID&lt;\/td&gt;<\/p>\n<p>&lt;td&gt;&lt;input name = &#8220;empid&#8221; type = &#8220;text&#8221;<\/p>\n<p>id = &#8220;empid&#8221;&gt;&lt;\/td&gt;<\/p>\n<p>&lt;\/tr&gt;<\/p>\n<p>&lt;tr&gt;<\/p>\n<p>&lt;td width = &#8220;100&#8221;&gt;Employee Salary&lt;\/td&gt;<\/p>\n<p>&lt;td&gt;&lt;input name = &#8220;salary&#8221; type = &#8220;text&#8221;<\/p>\n<p>id = &#8220;salary&#8221;&gt;&lt;\/td&gt;<\/p>\n<p>&lt;\/tr&gt;<\/p>\n<p>&lt;tr&gt;<\/p>\n<p>&lt;td width = &#8220;100&#8221;&gt; &lt;\/td&gt;<\/p>\n<p>&lt;td&gt; &lt;\/td&gt;<\/p>\n<p>&lt;\/tr&gt;<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0&lt;tr&gt;<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 &lt;td width = &#8220;100&#8221;&gt; &lt;\/td&gt;<\/p>\n<p>&lt;td&gt;<\/p>\n<p>&lt;input name = &#8220;update&#8221; type = &#8220;submit&#8221;<\/p>\n<p>id = &#8220;update&#8221; value = &#8220;Update&#8221;&gt;<\/p>\n<p>&lt;\/td&gt;<\/p>\n<p>&lt;\/tr&gt;<\/p>\n<p>&lt;\/table&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&lt;?php<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>After writing the program , execute in the browser , accept values from the user.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-295 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-145.png\" alt=\"\" width=\"546\" height=\"297\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-145.png 546w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-145-300x163.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-145-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-145-225x122.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-145-350x190.png 350w\" sizes=\"auto, (max-width: 546px) 100vw, 546px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here, we are accepting the amount and id to be updated and then you can go and check into the tables the trigger effects.<\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>We saw the above program with Mysqli_connect.<\/p>\n<p>&nbsp;<\/p>\n<p>Now , let us also have an example with PDO<\/p>\n<p>&nbsp;<\/p>\n<p><strong>UpdateSalary_pdo.PHP<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;title&gt;Trigger effect with Update EMp PDO&lt;\/title&gt; &lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form method = &#8220;post&#8221; action = &#8220;&lt;?php $_PHP_SELF ?&gt;&#8221;&gt; &lt;table width = &#8220;400&#8221; border =&#8221; 0&#8243; cellspacing = &#8220;1&#8221;<\/p>\n<p>cellpadding = &#8220;2&#8221;&gt;<\/p>\n<p>&lt;tr&gt;<\/p>\n<p>&lt;td width = &#8220;100&#8221;&gt;Employee ID&lt;\/td&gt;<\/p>\n<p>&lt;td&gt;&lt;input name = &#8220;empid&#8221; type = &#8220;text&#8221;<\/p>\n<p>id = &#8220;empid&#8221; value=&#8221;&#8221;&gt;&lt;\/td&gt;<\/p>\n<p>&lt;\/tr&gt;<\/p>\n<p>&lt;tr&gt;<\/p>\n<p>&lt;td width = &#8220;100&#8221;&gt;Employee Salary&lt;\/td&gt;<\/p>\n<p>&lt;td&gt;&lt;input name = &#8220;salary&#8221; type = &#8220;text&#8221;<\/p>\n<p>id = &#8220;salary&#8221; value=&#8221;&#8221;&gt;&lt;\/td&gt;<\/p>\n<p>&lt;\/tr&gt;<\/p>\n<p>&lt;tr&gt;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;td width = &#8220;100&#8221;&gt; &lt;\/td&gt;<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 &lt;td&gt; &lt;\/td&gt;<\/p>\n<p>&lt;\/tr&gt;<\/p>\n<p>&lt;tr&gt;<\/p>\n<p>&lt;td width = &#8220;100&#8221;&gt; &lt;\/td&gt;<\/p>\n<p>&lt;td&gt;<\/p>\n<p>&lt;input name = &#8220;update&#8221; type = &#8220;submit&#8221;<\/p>\n<p>id = &#8220;update&#8221; value = &#8220;Update&#8221;&gt;<\/p>\n<p>&lt;\/td&gt;<\/p>\n<p>&lt;\/tr&gt;<\/p>\n<p>&lt;\/table&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>$host = &#8216;localhost&#8217;;<\/p>\n<p>$dbname = &#8216;test&#8217;;<\/p>\n<p>$username = &#8216;root&#8217;;<\/p>\n<p>$password = &#8221;;<\/p>\n<p>if (isset ($_POST[&#8220;update&#8221;])){<\/p>\n<p>$empid = $_POST[&#8217;empid&#8217;];<\/p>\n<p>$salary = $_POST[&#8216;salary&#8217;];<\/p>\n<p>try {<\/p>\n<p>$connection = new<\/p>\n<p>PDO(&#8220;mysql:host=$host;dbname=$dbname&#8221;,$username,$password);<\/p>\n<p>$connection-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);<\/p>\n<p>$sql = &#8220;UPDATE emp SET salary=:salary WHERE empid=:empid&#8221;;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$statement = $connection-&gt;prepare($sql);<\/span><\/p>\n<\/div>\n<p>$statement-&gt;bindValue(&#8220;:empid&#8221;, $empid);<\/p>\n<p>$statement-&gt;bindValue(&#8220;:salary&#8221;, $salary);<\/p>\n<p>$count = $statement-&gt;execute();<\/p>\n<p>$connection = null;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Disconnect<\/p>\n<p>}<\/p>\n<p>catch(PDOException $e) {<\/p>\n<p>echo $e-&gt;getMessage();<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>?&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>You can run the above program in the browser and notice the effects.<\/p>\n<p>You can also add messages like Number of rows affected etc.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Integrating PHP and MySQL<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/yeiMgOpnBHM\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Additional Reading:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1) MySQL 5 for professionals , Ivan Bayross , Sharanam Shah, Shroff Publishers<\/p>\n<p style=\"text-align: justify\">2) MySQL documentation : dev.mysql.com\/doc\/<\/p>\n<p style=\"text-align: justify\">3) PHP and MySQL Web Development, \u201cLuke Welling, Laura Thomson\u201d,Pearson Publications.<\/p>\n","protected":false},"author":3,"menu_order":30,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["miss-bhumika-shah"],"pb_section_license":""},"chapter-type":[],"contributor":[60],"license":[],"class_list":["post-285","chapter","type-chapter","status-publish","hentry","contributor-miss-bhumika-shah"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/285","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":14,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/285\/revisions"}],"predecessor-version":[{"id":519,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/285\/revisions\/519"}],"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\/285\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=285"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=285"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=285"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}