{"id":1348,"date":"2014-08-17T20:00:44","date_gmt":"2014-08-18T00:00:44","guid":{"rendered":"http:\/\/www.jgarces.info\/?p=1348"},"modified":"2014-08-18T23:03:31","modified_gmt":"2014-08-19T03:03:31","slug":"connect-mysql-with-pdo","status":"publish","type":"post","link":"https:\/\/www.jgarces.info\/en\/connect-mysql-with-pdo\/","title":{"rendered":"Connect MySQL with PDO"},"content":{"rendered":"<p>Connection to MySQL using PDO library.<\/p>\n<pre lang=\"mysql\">\r\nCREATE TABLE `paciente` (\r\n  `paciente_rut` varchar(12) NOT NULL,\r\n  `paciente_nombre` varchar(100) NOT NULL,\r\n  `paciente_apellidop` varchar(100) NOT NULL,\r\n  `paciente_apellidom` varchar(100) default NULL,\r\n  `paciente_fono` varchar(50) default NULL,\r\n  `paciente_direccion` varchar(255) NOT NULL,\r\n  PRIMARY KEY  (`paciente_rut`)\r\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\r\n\r\nINSERT INTO `paciente` VALUES ('11111111-1', 'Pedro', 'Fuentes', 'Garc\u00eda', NULL, 'Santiago');\r\nINSERT INTO `paciente` VALUES ('22222222-2', 'Juan', 'Santana', 'Bustos', NULL, 'Santiago');\r\nINSERT INTO `paciente` VALUES ('33333333-3', 'Carolina', 'Soto', 'Barra', NULL, 'Santiago');\r\n<\/pre>\n<p><!--more--><\/p>\n<pre lang=\"php\">\r\n<?php\r\n$scon = \"mysql:dbname=clinica;host=localhost\";\r\n$suser = \"curso\";\r\n$spass = \"curso\";\r\n\r\n$conexion = null;\r\ntry\r\n{\r\n\t$conexion = new PDO($scon, $suser, $spass);\r\n}\r\ncatch(PDOException $ex)\r\n{\r\n\techo \"Connection error: \" . $ex->getMessage();\r\n}\r\n\r\n$sql = \"SELECT * FROM paciente\";\r\n\r\n$rs = $conexion->query($sql);\r\n$datos = $rs->fetchAll(PDO::FETCH_ASSOC);\r\n\r\nforeach($datos as $paciente)\r\n{\r\n\techo \"Paciente TAX Id: \" . $paciente[\"paciente_rut\"] . \"<br \/>\";\r\n\techo \"Paciente Name: \" . $paciente[\"paciente_nombre\"] . \"<br \/><br \/>\";\r\n}\r\n\r\n$datos = null;\r\n$rs = null;\r\n$conexion = null;\r\n?>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Connection to MySQL using PDO library. CREATE TABLE `paciente` ( `paciente_rut` varchar(12) NOT NULL, `paciente_nombre` varchar(100) NOT NULL, `paciente_apellidop` varchar(100) NOT NULL, `paciente_apellidom` varchar(100) default NULL, `paciente_fono` varchar(50) default NULL, `paciente_direccion` varchar(255) NOT NULL, PRIMARY KEY (`paciente_rut`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `paciente` VALUES (&#8216;11111111-1&#8217;, &#8216;Pedro&#8217;, &#8216;Fuentes&#8217;, &#8216;Garc\u00eda&#8217;, NULL, &#8216;Santiago&#8217;); INSERT INTO `paciente` VALUES [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[38],"class_list":["post-1348","post","type-post","status-publish","format-standard","hentry","category-general-en-2","tag-basic-programming"],"_links":{"self":[{"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/posts\/1348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/comments?post=1348"}],"version-history":[{"count":1,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/posts\/1348\/revisions"}],"predecessor-version":[{"id":1349,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/posts\/1348\/revisions\/1349"}],"wp:attachment":[{"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/media?parent=1348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/categories?post=1348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jgarces.info\/en\/wp-json\/wp\/v2\/tags?post=1348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}