Advertisement
Guest User

Untitled

a guest
Dec 19th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <!--
  3. To change this license header, choose License Headers in Project Properties.
  4. To change this template file, choose Tools | Templates
  5. and open the template in the editor.
  6. -->
  7. <html>
  8. <head>
  9. <meta charset="UTF-8">
  10. <title></title>
  11. </head>
  12. <body>
  13. <h1>Acceso a datos con PDO desde PHP</h1>
  14. <?php
  15. // PASO 1: Conexion a la base de datos con control de errores
  16.  
  17. try {
  18. echo 'Conectando con la base de datos ...';
  19. // Crear objeto de conexion a la base de datos
  20. $dsn = "mysql:host=localhost;dbname=world;charset=utf8";
  21. $username = "root";
  22. $password = "Temp2016$$";
  23. $db = new PDO($dsn, $username, $password);
  24. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  25. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
  26. echo 'Conexion establecida.';
  27. } catch (PDOException $e) {
  28. echo 'Error de conexion a la base de datos.' .' <BR>';
  29. echo $e->getMessage();
  30. }
  31. //paso # 2. Lanzar consultas simplres (SIN PARAMETROS) contra la base de datos
  32. //PRIMERO: consultas de seleccion
  33. //Siempre se utiliza el metodo query()
  34. // Recuperar los registros de la tabla City (Lanzar la consulta
  35. // desde el cliente de MySQL y si esta bien, copiarla y pegarla
  36. // en el codigo de PHP)
  37. //Variante 1.
  38. $consulta1 = "select * from City";
  39. $db->query(consulta1);
  40. ?>
  41. </body>
  42. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement