/ Published in: PHP
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
By bakkelun
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
-- mysql connection handler -- <? var $dbase; var $dbcon; var $db_user; var $db_pw; /* Constructor */ function __construct($base,$user,$pw) { $this->dbase = $base; $this->db_user = $user; $this->db_pw = $pw; if (!$this->dbcon) { } else { } } } // connect end function closeconnection() { } // closeconnection end } ?> --- sql data object --- <? require('path to the above mysql class file'); var $resultSet; var $update_string; /** * Gets SQL data. Default limit is 10 (if no param passed) to avoid erronous and SERVER-heavy loads. * */ function getData($table,$what_to_get,$where,$orderby,$sortorder='DESC',$offset='0',$limit='10',$outputArray='0') { if (!$query) { } // Output as array if ($outputArray=='1') { $this->resultSet[] = $query_row; } // Output as mysql-resource. Useful if you have existing code logic that depends on a query resource result } else { $this->resultSet = $query; } } /* Handle potensially dangerous input */ function safevalue ($value) { } } ?> ---- EXAMPLE USAGE ---- Let's say we want to output all the info in our 'newstable' from our database 'example-database'. Here's how: <? require('path-to-the-sql-data-object-file.php'); $sqlData = new sqlDataObject('example_database','username','password'); /* Get all news info where the id is less than 10 and ordr by the id. */ /* In this example let's say the database has only three columns: id, heading, message */ $some_news_output = $sqlData->getData('newstable','*','id<10','id'); echo('<h1>'.$news_row['heading'].'</h1>'. $news_row['message'].' <p><a href="?page=news&id="'.$news_row['id'].'">Read more about this</a></p>'); } ?>