Guest User

Untitled

a guest
Aug 11th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. PHP database selection issue
  2. <?php
  3. $dbhost = "localhost";
  4. $dbuser = "root";
  5. $dbpass = "";
  6. $dbname = "test";
  7. ?>
  8.  
  9. <?php
  10. class Database {
  11.  
  12. //-------------------------------------------
  13. // Connects to the database
  14. //-------------------------------------------
  15. function connect() {
  16. if (isset($dbhost) && isset($dbuser) && isset($dbpass) && isset($dbname)) {
  17. $con = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect: " . mysql_error());
  18. $selected_db = mysql_select_db($dbname, $con) or die("Could not select test DB");
  19. }
  20. }// end function connect
  21. } // end class Database
  22. ?>
  23.  
  24. <?php
  25. // include the config file and database class
  26. include 'config.php';
  27. include 'database.php';
  28.  
  29. ...
  30. ?>
  31.  
  32. <?php
  33. // include the news class
  34. include 'news.php';
  35. // create an instance of the Database class and call it $db
  36. $db = new Database;
  37. $db -> connect();
  38.  
  39. class BLnews {
  40.  
  41. function getNews() {
  42. $sql = "SELECT * FROM news";
  43. if (isset($sql)) {
  44. $result = mysql_query($sql) or die("Could not execute query. Reason: " .mysql_error());
  45. }
  46. return $result;
  47. }
  48. ?>
  49.  
  50. <?php
  51. ...
  52.  
  53. include 'includes/BLnews.php';
  54. $blNews = new BLnews();
  55. $news = $blNews->getNews();
  56. ?>
  57.  
  58. ...
  59. <?php
  60. while($row = mysql_fetch_array($news))
  61. {
  62. echo '<div class="post">';
  63. echo '<h2><a href="#"> ' . $row["title"] .'</a></h2>';
  64. echo '<p class="post-info">Posted by <a href="#"> </a> | <span class="date"> Posted on <a href="#">' . $row["date"] . '</a></span></p>';
  65. echo $row["content"];
  66. echo '</div>';
  67. }
  68. ?>
Add Comment
Please, Sign In to add comment