Posted By

bakkelun on 07/16/09


Tagged

sql oop php5


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


sqlDataObj in PHP5


 / Published in: PHP
 

URL: http://www.dosspirit.net

Pretty straight forward implementation of mysql instantiation using php5 oop. Example usage at the bottom. The database in the example is named 'example_database' and has 1 table, 'newstable'. This table has three columns: 'id,heading,message'.

By bakkelun

  1. -- mysql connection handler --
  2.  
  3. <?
  4.  
  5. class mysql {
  6. var $dbase;
  7. var $dbcon;
  8. var $db_user;
  9. var $db_pw;
  10.  
  11. /* Constructor */
  12. function __construct($base,$user,$pw) {
  13.  
  14. $this->dbase = $base;
  15. $this->db_user = $user;
  16. $this->db_pw = $pw;
  17.  
  18. $this->dbcon = mysql_connect("url-to-your-db",$this->db_user,$this->db_pw);
  19. if (!$this->dbcon) {
  20. die( "<p>Unable to connect to the database server at this time.</p>" );
  21. } else {
  22. if (!@mysql_select_db($this->dbase,$this->dbcon)) {
  23. die( "<p>Could not choose this database: ".$this->dbase." Please try again later.</p>" );
  24. }
  25. }
  26. } // connect end
  27.  
  28.  
  29. function closeconnection() {
  30. mysql_close($this->dbcon);
  31. } // closeconnection end
  32.  
  33. }
  34. ?>
  35.  
  36.  
  37. --- sql data object ---
  38.  
  39. <?
  40. require('path to the above mysql class file');
  41.  
  42. class sqlDataObject extends mysql {
  43.  
  44. var $resultSet;
  45. var $update_string;
  46.  
  47. /**
  48. * Gets SQL data. Default limit is 10 (if no param passed) to avoid erronous and SERVER-heavy loads.
  49. *
  50. */
  51. function getData($table,$what_to_get,$where,$orderby,$sortorder='DESC',$offset='0',$limit='10',$outputArray='0') {
  52.  
  53. if (!empty($where)) { $where = 'WHERE '.$where; }
  54.  
  55. if (!empty($orderby)) { $orderby = 'ORDER BY '.$orderby; }
  56.  
  57. if (!empty($limit)) { $limit = 'LIMIT '.$offset.','.$limit; }
  58.  
  59. $query = mysql_query("SELECT $what_to_get FROM $table $where $orderby $sortorder $limit");
  60. if (!$query) {
  61. $this->errorHandler(mysql_error());
  62. }
  63.  
  64. // Output as array
  65. if ($outputArray=='1') {
  66. while ($query_row = mysql_fetch_assoc($query)) {
  67. $this->resultSet[] = $query_row;
  68. }
  69.  
  70. // Output as mysql-resource. Useful if you have existing code logic that depends on a query resource result
  71. } else {
  72. $this->resultSet = $query;
  73. }
  74. }
  75.  
  76. /* Handle potensially dangerous input */
  77. function safevalue ($value) {
  78. return mysql_real_escape_string(strip_slashes(trim($value)));
  79. }
  80.  
  81. }
  82. ?>
  83.  
  84. ---- EXAMPLE USAGE ----
  85.  
  86. Let's say we want to output all the info in our 'newstable' from our database 'example-database'. Here's how:
  87.  
  88. <?
  89. require('path-to-the-sql-data-object-file.php');
  90.  
  91. $sqlData = new sqlDataObject('example_database','username','password');
  92.  
  93.  
  94.  
  95.  
  96. /* Get all news info where the id is less than 10 and ordr by the id. */
  97.  
  98. /* In this example let's say the database has only three columns: id, heading, message */
  99.  
  100. $some_news_output = $sqlData->getData('newstable','*','id<10','id');
  101.  
  102. while ($news_row = mysql_fetch_array($some_news_output)) {
  103. echo('<h1>'.$news_row['heading'].'</h1>'.
  104. $news_row['message'].'
  105. <p><a href="?page=news&amp;id="'.$news_row['id'].'">Read more about this</a></p>');
  106. }
  107. ?>

Report this snippet  

You need to login to post a comment.