Advertisement
Guest User

Untitled

a guest
Sep 1st, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. <?php
  2.  
  3. /*#############################
  4. * Developer: Mohammad Sharaf Ali
  5. * Designation: Web Developer
  6. * Version: 1.0
  7. */#############################
  8.  
  9.  
  10. // LIBRARIES
  11. require_once 'db/NotORM.php';
  12.  
  13.  
  14. // CONSTANTS
  15. const DB_HOST = 'your-host';
  16. const DB_USER = 'your-user';
  17. const DB_PASS = 'your-pass';
  18. const DB_NAME = 'your-dbname';
  19. const DB_TABLE = 'your-table';
  20. const DB_DSN = 'mysql:dbname='. DB_NAME. ';host='. DB_HOST;
  21.  
  22.  
  23. // DB STRUCTURE
  24. $structure = new NotORM_Structure_Convention(
  25. $primary = 'ID',
  26. $foreign = '%s_ID',
  27. $table = '%s',
  28. $prefix = '');
  29.  
  30. $pdo = null;
  31. $db = null;
  32.  
  33.  
  34. // HELPER METHODS
  35. function dbConnect() {
  36. global $pdo;
  37. global $db;
  38. global $structure;
  39.  
  40. $pdo = new PDO(DB_DSN, DB_USER, DB_PASS); // array(PDO::ATTR_PERSISTENT => true) // make persistent connection
  41. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  42. $db = new NotORM($pdo, $structure); // new NotORM_Cache_Session()); // enable NotORM caching
  43. }
  44.  
  45. function dbClose() {
  46. global $pdo;
  47. global $db;
  48.  
  49. $pdo = null;
  50. $db = null;
  51. }
  52.  
  53. function toScreen($data, $exit = 0, $escape = 0) {
  54. echo '<pre>';
  55.  
  56. if (is_array($data)) {
  57. print_r($data);
  58. } else {
  59. if ($escape) {
  60. echo nl2br($data);
  61. } else {
  62. echo $data;
  63. }
  64. }
  65.  
  66. echo '</pre>';
  67.  
  68. ob_flush();
  69. flush();
  70.  
  71. if ($exit) {
  72. exit;
  73. }
  74. }
  75.  
  76.  
  77. // MAIN
  78. dbConnect();
  79.  
  80. $data = array(
  81. 'fld1' => 'val1',
  82. 'fld2' => 'val2',
  83. 'fld3' => 'val3',
  84. 'fld4' => 'val4'
  85. );
  86.  
  87. $resultInsert = $db->yourtablename()->insert($data); // you cannot get insert sql
  88. toScreen(iterator_to_array($resultInsert)); // notorm object to assoc array
  89.  
  90. $resultSelect = $db->yourtablename(); // you can get select sql e.g. echo $db->yourtablename();
  91. toScreen(array_map('iterator_to_array', iterator_to_array($resultSelect))); // notorm object to assoc array, you can also use foreach loop
  92.  
  93. dbClose();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement