Advertisement
LrdArc

csc_pdo sqlsrv

Jun 21st, 2016
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. <?php
  2. /**
  3. * csc_pdo class
  4. * It's part of Lrdc Framework,
  5. * But you can use it separately.
  6. *
  7. * How to use:
  8. * - Fetching 1 row
  9. * with mysql_query:
  10. * $result = mysql_query( "SELECT * FROM table WHERE column='$value'" );
  11. * $fetch = mysql_fetch_object( $result );
  12. * echo $fetch->column;
  13. *
  14. * with csc_pdo:
  15. * $f = $db->f( 'table', '*', 'WHERE column=?', $value );
  16. * echo $f->column;
  17. *
  18. * - Fetching multiple row
  19. * with mysql_query:
  20. * $query = "SELECT column FROM table WHERE column1='$value1' AND column2='$value2'";
  21. * $result = mysql_query( $query );
  22. * while ( $row = mysql_fetch_assoc( $result ) ) {
  23. * echo $row['column'] . '<br>';
  24. * }
  25. *
  26. * with csc_pdo:
  27. * $f = $db->r( 'table', 'column', 'WHERE column1=? AND column2=?', array( $value1, $value2 ) );
  28. * foreach ( $f as $r ) {
  29. * echo $r['column'] . '<br>';
  30. * }
  31. *
  32. */
  33.  
  34. class csc_pdo {
  35. public $db_name;
  36. public $pfx;
  37. protected $pfx_;
  38. protected $dbh;
  39. private $db_user;
  40. private $db_pass;
  41.  
  42. public function __construct( $db_user, $db_pass, $db_name ) {
  43. $this->db_name = $db_name;
  44. $this->db_user = $db_user;
  45. $this->db_pass = $db_pass;
  46. $this->connect( $db_name );
  47. }
  48.  
  49. // Connect to SQL
  50. public function connect( $db_name ) {
  51. $db_name = ( $db_name != '' ) ? ';Database=' . $db_name : '';
  52. $dsn = 'sqlsrv:Server=localhost' . $db_name;
  53.  
  54. try {
  55. $this->dbh = new PDO( $dsn, $this->db_user, $this->db_pass );
  56. } catch ( PDOException $e ) {
  57. return false;
  58. }
  59. }
  60.  
  61. // Temporary table pfx
  62. public function pfx( $pfx ) {
  63. if ( $pfx == 'roll' ) {
  64. $this->pfx = $this->pfx_;
  65. $this->pfx_ = null;
  66. } else {
  67. $this->pfx_ = $this->pfx;
  68. $this->pfx = $pfx;
  69. }
  70. }
  71.  
  72. // Determine table names
  73. public function get_table( $table ) {
  74. $table = $this->pfx . $table;
  75. if ( $this->pfx_ ) $this->pfx( 'roll' );
  76. return $table;
  77. }
  78.  
  79. // Any Query
  80. public function query( $query, $array=null ) {
  81. $array = ( ! is_array( $array ) && $array ) ? array( $array ) : $array;
  82. $sth = $this->dbh->prepare( $query );
  83. return $sth->execute( $array );
  84. }
  85.  
  86. // Do fetch
  87. public function f ( $table, $field, $options=null, $array=null ) {
  88. $array = ( ! is_array( $array ) && $array ) ? array( $array ) : $array;
  89. $table = $this->get_table( $table );
  90. $sth = $this->dbh->prepare( "SELECT $field FROM $table $options" );
  91. $sth->execute( $array );
  92. return $sth->fetch( PDO::FETCH_OBJ );
  93. }
  94.  
  95. // Select as row
  96. public function r ( $table, $field, $options=null, $array=null ) {
  97. $array = ( ! is_array( $array ) && $array ) ? array( $array ) : $array;
  98. $table = $this->get_table( $table );
  99. $sth = $this->dbh->prepare( "SELECT $field FROM $table $options" );
  100. $sth->execute( $array );
  101. while ( $r[] = $sth->fetch( PDO::FETCH_ASSOC ) );
  102. if ( empty( $r[count( $r ) - 1] ) ) unset( $r[count( $r ) - 1] );
  103. return $r;
  104. }
  105.  
  106. // Insert Query
  107. public function i ( $table, $field, $array=null ) {
  108. $array = ( ! is_array( $array ) && $array ) ? array( $array ) : $array;
  109. for ( $x=1; $x<=count( $array ); $x++ ) $f[] = '?';
  110. $val = implode( ',', $f );
  111. $table = $this->get_table( $table );
  112. $sth = $this->dbh->prepare( "INSERT INTO $table ($field) VALUES ($val)" );
  113. $sth->execute( $array );
  114. return $this->dbh->lastInsertId();
  115. }
  116.  
  117. // Update Query
  118. public function u ( $table, $options=null, $array=null ) {
  119. $array = ( ! is_array( $array ) && $array ) ? array( $array ) : $array;
  120. $table = $this->get_table( $table );
  121. $sth = $this->dbh->prepare( "UPDATE $table $options" );
  122. $sth->execute( $array );
  123. }
  124.  
  125. // Delete Query
  126. public function d ( $table, $options=null, $array=null ) {
  127. $array = ( ! is_array( $array ) && $array ) ? array( $array ) : $array;
  128. $table = $this->get_table( $table );
  129. $sth = $this->dbh->prepare( "DELETE FROM $table $options" );
  130. $sth->execute( $array );
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement