connect, retrieve, insert, disconnect class


/ Published in: PHP
Save to your folder(s)

will change to be more specific to what i want l8ter. Just basic at the moment.


Copy this code and paste it in your HTML
  1. <?php
  2. //created by me...
  3.  
  4. class apple {
  5. // connect to, and select the database
  6. private function connectDB(){
  7. $dbconnect = mysql_connect('localhost', 'root','', 'form')
  8. or die('no connection');
  9. mysql_select_db("form");
  10. echo 'connected';
  11. }
  12.  
  13. // get table names
  14. function gettables(){
  15. $this->connectDB();
  16. $query = 'SHOW TABLES from form';
  17. $result = mysql_query($query);
  18. $num_rows = mysql_num_rows($result);
  19. for ($i = 0; $i < $num_rows; $i++) {
  20. echo "<p>Table: ", mysql_tablename($result, $i), "</p>\n";
  21. }
  22. }
  23.  
  24. // list articles
  25. function goober() {
  26. $this->connectDB();
  27. $result = mysql_query("SELECT * FROM article");
  28. while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
  29. print("<h2>".$row['title']."</h2>");
  30. print($row["content"]."<br />");
  31. }
  32. }
  33.  
  34. // insert article
  35. function insertArticle(){
  36. $sql = mysql_query("INSERT INTO article (title, content) VALUES('class1', 'tree1')")
  37. or die;
  38. }
  39.  
  40. // disconnect
  41. function disconnectDB(){
  42. }
  43. }
  44.  
  45. $art= new apple(); // instantiate the class
  46. $art->goober(); //retrieves articles and displays them
  47. //$art->insertArticle();
  48. $art->disconnectDB();
  49.  
  50. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.