SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot JonathanS's Avatar
    Join Date
    Jun 2004
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Anthology -> Using OOP to Fetch (Error!)

    Howdy all; I picked up the PHP Anthology books (both Vols.)
    around my birthday in early July. I never even picked them
    up once I saw it was all in OOP (@ the time, didn't know OOP).

    Fast forwarding, it finally just *clicked*-- the whole OOP thing
    thanks to a link someone gave me.

    Now, I used the class just fine to query just once, find
    how many records, etc. ~ basicly all the examples.

    However, some pages I'm using more than one query
    and this is where my troubles begin.

    Fatal error: Call to a member function query() on a non-object in E:\xampp\xampp\htdocs\actual\Last Entry\blog.php on line 64
    Code:
    <?php
    session_start();
    
    // Set the page title
    $title = "Last Entry.com - Home";
    
    // Set the Description Meta Data
    $dmeta = "Description Meta Data";
    
    // Set the Keyword Meta Data
    $kmeta = "Keyword Meta Data";
    
    // Require the class fil
    require_once('rf/class.php');
    
    // Require the News Page System
    require_once('rf/nps.php');
    
    // Require the header file
    require_once('rf/head.php');
    
    $blog = $_GET['blog'];
    
    if (isset($blog)) {
    
    // Connect to MySQL
    $db = & new MySQL($host,$dbUser,$dbPass,$dbName);
    
    // Set up the query...
    $gdnps = "SELECT id, author, title, msg, email, DATE_FORMAT(date, '%h:%i - %m.%d.%y'), blog";
    $gdnps .= " FROM gdnps WHERE blog='$blog' ORDER BY id DESC LIMIT $from, $max_results";
    
    // Perform said query
    $result = $db->query($gdnps);
    while ($row = $result->fetch()) {
    
    $id = $row['id'];
    $entryauthor = $row['author'];
    $entrytitle = $row['title'];
    $entrytext = $row['msg'];
    $email = $row['email'];
    $date = $row['date'];
    
    $cresults = mysql_query("SELECT * FROM comments WHERE news_id='$id' ");
    $num_rows = mysql_num_rows($cresults);
    
    	// Build the output right here
    echo "<div class=\"title\">$entrytitle</div>";
    
    echo "<div class=\"normal\">[ Posted by $entryauthor @ $date | ";
    echo "<a href=\"comments.php?id=$id\" class=\"doc\">Read</a> / ";
    echo "<a href=\"reply.php?id=$id\" class=\"doc\">Reply</a> / $num_rows Replies ] </div>";
    
    	}
    } else { 
    
    // send a query to the mySQL database
    $sql = "SELECT id, blog FROM blog ORDER BY blog ASC";
    $result2 = $db->query($sql);
    
    while ($row2 = $result2->fetch()) {
    $id2 = $row2['id'];
    $blog2 = $row2['blog'];
    
    echo "<div class=\"title\">Current List of Blogs</div>";
    echo "<div class=\"normal\">$blog2 -> <a href=\"blog.php?blog=$blog2\" class=\"doc\">Click here to view</a> <br /></div>";
    
    }
    	}
    
    require_once('rf/np.php');
    require_once('rf/foot.php');
    
    ?>
    Nothing overtly fancy; anyways, any idea how to get around this?
    I'm still new to OOP, so I can't figure this one out

  2. #2
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Staffordshire, UK & Florida, USA
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have an if/else construct but you are only creating a database connection if the test condition is true (isset($blog)) - if the test fails and the else section is executed you don't have a database connection and are executing a query on an unset variable, hence the error message.

    You could move the line that creates the database connection above the if statement to rectify this.

  3. #3
    SitePoint Zealot JonathanS's Avatar
    Join Date
    Jun 2004
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! Did the trick


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •