Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PHP database selection issue
- <?php
- $dbhost = "localhost";
- $dbuser = "root";
- $dbpass = "";
- $dbname = "test";
- ?>
- <?php
- class Database {
- //-------------------------------------------
- // Connects to the database
- //-------------------------------------------
- function connect() {
- if (isset($dbhost) && isset($dbuser) && isset($dbpass) && isset($dbname)) {
- $con = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect: " . mysql_error());
- $selected_db = mysql_select_db($dbname, $con) or die("Could not select test DB");
- }
- }// end function connect
- } // end class Database
- ?>
- <?php
- // include the config file and database class
- include 'config.php';
- include 'database.php';
- ...
- ?>
- <?php
- // include the news class
- include 'news.php';
- // create an instance of the Database class and call it $db
- $db = new Database;
- $db -> connect();
- class BLnews {
- function getNews() {
- $sql = "SELECT * FROM news";
- if (isset($sql)) {
- $result = mysql_query($sql) or die("Could not execute query. Reason: " .mysql_error());
- }
- return $result;
- }
- ?>
- <?php
- ...
- include 'includes/BLnews.php';
- $blNews = new BLnews();
- $news = $blNews->getNews();
- ?>
- ...
- <?php
- while($row = mysql_fetch_array($news))
- {
- echo '<div class="post">';
- echo '<h2><a href="#"> ' . $row["title"] .'</a></h2>';
- echo '<p class="post-info">Posted by <a href="#"> </a> | <span class="date"> Posted on <a href="#">' . $row["date"] . '</a></span></p>';
- echo $row["content"];
- echo '</div>';
- }
- ?>
Add Comment
Please, Sign In to add comment