Guest User

Untitled

a guest
Nov 29th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. id name
  2. 1 Herbert
  3. 2 LG
  4. 3 Levins
  5.  
  6. <?php
  7.  
  8. $dsn = 'mysql:host=localhost;dbname=postgre';
  9. $username = 'root';
  10. $password = '';
  11. $options = array(
  12. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  13. );
  14.  
  15. $db = new PDO($dsn, $username, $password, $options);
  16.  
  17. $stmt = $db->query("SELECT * FROM friends");
  18. $cnt_columns = $stmt->columnCount();
  19. for($i = 0; $i < $cnt_columns; $i++) {
  20. $metadata = $stmt->getColumnMeta($i);
  21. var_dump($metadata);
  22. }
  23.  
  24. ?>
  25.  
  26. array
  27. 'native_type' => string 'LONG' (length=4)
  28. 'pdo_type' => int 2
  29. 'flags' =>
  30. array
  31. empty
  32. 'table' => string 'friends' (length=7)
  33. 'name' => string 'id' (length=2)
  34. 'len' => int 11
  35. 'precision' => int 0
  36.  
  37. array
  38. 'native_type' => string 'VAR_STRING' (length=10)
  39. 'pdo_type' => int 2
  40. 'flags' =>
  41. array
  42. empty
  43. 'table' => string 'friends' (length=7)
  44. 'name' => string 'name' (length=4)
  45. 'len' => int 60
  46. 'precision' => int 0
  47.  
  48. id name
  49. 1 Hebert
  50. 2 LG
  51. 3 Levins
  52.  
  53. $stmt = $db->query("SELECT * FROM friends");
  54. $cnt_columns = $stmt->columnCount();
  55. for($i = 0; $i < $cnt_columns; $i++) {
  56. $metadata = $stmt->getColumnMeta($i);
  57. var_dump($metadata);
  58. }
  59.  
  60. $sql = "SELECT * FROM friends";
  61. $stmt = $db->prepare($sql);
  62. $stmt->execute();
  63. $result = $stmt->fetchAll();
  64.  
  65. // field names
  66. if(count($result) > 0) {
  67. foreach($result[0] as $k => $v) {
  68. if(!is_int($k)) {
  69. echo $k . "t";
  70. }
  71. }
  72. }
  73. echo PHP_EOL;
  74.  
  75. // data
  76. foreach ($result as $row) {
  77. foreach($row as $k => $v) {
  78. if(!is_int($k)) {
  79. echo $row[$k] . "t";
  80. }
  81. }
  82. echo PHP_EOL;
  83. }
Add Comment
Please, Sign In to add comment