Having trouble getting info from database to display

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tcreynolds
Forum Newbie
Posts: 4
Joined: Wed Jun 23, 2010 2:20 pm

Having trouble getting info from database to display

Post by tcreynolds »

So, I'm building a simple blog, and am having trouble getting the date to show up. I believe it is querying the database since the error message is no longer showing up. The database does have data in it. I'm a php novice and any help is VERY appreciated.Below is the code for the three files:

First, the page where the data is displayed:

Code: Select all

<?php
	include ("includes.php");
	
	$blogPosts = GetBlogPosts();
	
	foreach ($blogPosts as $post)
	{
		echo "<div class='post'>";
		echo "<h2>" . $post->title . "</h2>";
		echo "<p>" . $post->post . "</p>";
		echo "<span class='footer'>Posted By: " . $post->author . " Posted On: " . $post->datePosted . " Tags: " . $post->tags . "</span>";
		echo "</div>";
	}
	?>
Second, the includes.php file referenced above:

Code: Select all

<?php 

include 'blogpost.php';

$connection = mysql_connect('***', '***', '***') or die ("<p class='error'>Sorry, we were unable to connect to the database server.</p>");  
$database = "phptestblog";  
mysql_select_db($database, $connection) or die ("<p class='error'>Sorry, we were unable to connect to the database.</p>");  

function GetBlogPosts($inId=null, $inTagId=null)  
{  
    if (!empty($inId))  
    {  
        $query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC");  
    }  
    else if (!empty($inTagId))  
    {  
        $query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC");  
    }  
    else  
    {  
        $query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");  
    }  
  
    $postArray = array();  
    while ($row = mysql_fetch_assoc($query))  
    {  
        $myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row["author_id"], $row['dateposted']);  
        array_push($postArray, $myPost);  
    }  
    return $postArray;  
}  

?>
And finally, the blogpost.php file referenced in the includes file:

Code: Select all

<?php

class Blogpost
{
	public $id;
	public $title;
	public $post;
	public $author;
	public $date;
	public $tags;
}

function __construct($inId=null, $inTitle=null, $inPost=null, $inPostFull=null, $inAuthorId=null, $inDatePosted=null)  
{  
    if (!empty($inId))  
    {  
        $this->id = $inId;  
    }  
    if (!empty($inTitle))  
    {  
        $this->title = $inTitle;  
    }  
    if (!empty($inPost))  
    {  
        $this->post = $inPost;  
    }  
  
    if (!empty($inDatePosted))  
    {  
        $splitDate = explode("-", $inDatePosted);  
        $this->date_Posted = $splitDate[1] . "/" . $splitDate[2] . "/" . $splitDate[0];  
    }  
  
    if (!empty($inAuthorId))  
    {  
        $query = mysql_query("SELECT first_name, last_name FROM people WHERE id = " . $inAuthorId);  
        $row = mysql_fetch_assoc($query);  
        $this->author = $row["first_name"] . " " . $row["last_name"];  
    }  
  
    $postTags = "No Tags";  
    if (!empty($inId))  
    {  
        $query = mysql_query("SELECT tags.* FROM blog_post_tags LEFT JOIN (tags) ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = " . $inId);  
        $tagArray = array();  
        $tagIDArray = array();  
        while($row = mysql_fetch_assoc($query))  
        {  
            array_push($tagArray, $row["name"]);  
            array_push($tagIDArray, $row["id"]);  
        }  
        if (sizeof($tagArray) > 0)  
        {  
            foreach ($tagArray as $tag)  
            {  
                if ($postTags == "No Tags")  
                {  
                    $postTags = $tag;  
                }  
                else  
                {  
                    $postTags = $postTags . ", " . $tag;  
                }  
            }  
        }  
    }  
    $this->tags = $postTags;  
}   
  
?>  
tcreynolds
Forum Newbie
Posts: 4
Joined: Wed Jun 23, 2010 2:20 pm

Re: Having trouble getting info from database to display

Post by tcreynolds »

So...just figured out how to check the contents of the array, and it's giving me:

Array ( [0] => Blogpost Object ( [id] => [title] => [post] => [author] => [date] => [tags] => ) )

Not sure if that helps any, but I think it might not be getting the data from the database somehow.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Having trouble getting info from database to display

Post by AbraCadaver »

Your original post appears to have actual hostname, username and password. I would edit them out if I were you.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Having trouble getting info from database to display

Post by John Cartwright »

AbraCadaver wrote:Your original post appears to have actual hostname, username and password. I would edit them out if I were you.
Removed.
Post Reply