Guest User

Untitled

a guest
Dec 8th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. <?php
  2.  
  3. class MySQLDatabase{
  4.  
  5. private $con;
  6. private $database="localhost";
  7. private $username="root";
  8. private $password="root";
  9. private $databasename="blog";
  10.  
  11. function __construct(){
  12. $this->open_connection();
  13. }
  14.  
  15. public function open_connection(){
  16. $this->con=mysql_connect($this->database,$this->username,$this->password);
  17. if(!$this->con){
  18. die("Database connection failed: ".mysql_error());
  19. }else{
  20. $db_select=mysql_select_db($this->databasename,$this->con);
  21. if(!$db_select){
  22. die("Database selection failed: ".mysql_error());
  23. }
  24. }
  25. }
  26. /*public function close_connection(){
  27. if(isset($this->con)){
  28. mysql_close($this->con);
  29. unset($this->con);
  30. }
  31. }*/
  32. }
  33.  
  34. //$database=new MySQLDatabase();
  35. //$database->close_connection();
  36.  
  37.  
  38. class Posts {
  39. private $posts;
  40.  
  41. function postPost($title, $thetext, $usr) {
  42. $date = time();
  43. $date = date("Y-m-d", $date);
  44. $query = "INSERT INTO posts (
  45. id,
  46. `date`,
  47. `title`,
  48. `thetext`,
  49. `usr`
  50. ) VALUES (
  51. NULL,
  52. '$date',
  53. '$title',
  54. '$thetext',
  55. '$usr'
  56. )";
  57. $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  58. }
  59.  
  60. function getPosts() {
  61. $query = "SELECT * FROM posts ORDER BY id DESC";
  62. $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  63. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  64. $this->posts[] = $row;
  65. }
  66. return $this->posts;
  67. }
  68. }
  69.  
  70. $blogs = new Posts();
  71.  
  72. $usr = "Lovisa";
  73. $title = "En blogg";
  74. $thetext = "Min bloggtext...";
  75.  
  76. $blogs->postPost($title, $thetext, $usr);
  77.  
  78. $posts = $blogs->getPosts();
  79.  
  80. function showPosts() {
  81. foreach($posts as $each_post) {
  82. $id = $each_post["id"];
  83. $date = $each_post["date"];
  84. $title = $each_post["title"];
  85. $thetext = $each_post["thetext"];
  86. $usr = $each_post["usr"];
  87. ?>
  88.  
  89. <div>
  90. <?=$id?>, <?=$date?>, <?=$title?>, <?=$thetext?>, <?=$usr?>
  91. </div>
  92.  
  93. <?php
  94. }
  95. }
  96.  
  97. ?>
  98.  
  99. <!DOCTYPE HTML>
  100. <html lang="sv">
  101. <head>
  102. <title></title>
  103. </head>
  104. <body>
  105.  
  106. <?php showPosts(); ?>
  107.  
  108. </body>
  109. </html>
Add Comment
Please, Sign In to add comment