Simple question-how to display contents of an array.

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

Simple question-how to display contents of an array.

Post by tcreynolds »

I have code that retrieves information from a mySql database and puts it into an array. I want to know if it's actually pulling the information into my array by creating a code (maybe using foreach?) that will display all of the values in the array. Very new to sql and using someone else's code, but thinking this should be simple for any experienced php programmer.

Here's what I think is the relevant code:

Code: Select all

 $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;  
The query is just what I got from the query in my database. This is all then put into a variable:

Code: Select all

$blogPosts = GetBlogPosts();
I didn't include the part of the GetBlogPosts() function that contains the query because I didn't think it was useful. Then again, very new to php (day 2).Let me know if you need any more information.

So, any help on how to check to see what's in my $blogPosts would be awesome, particularly if you can tell me how to display the full contents so I can do this with problems in the future too. Thanks so much for taking the time to help!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Simple question-how to display contents of an array.

Post by AbraCadaver »

Just to check it:

Code: Select all

print_r()
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.
tcreynolds
Forum Newbie
Posts: 4
Joined: Wed Jun 23, 2010 2:20 pm

Re: Simple question-how to display contents of an array.

Post by tcreynolds »

Wow. That worked great! I think it's not getting the information somehow, since it gave me:

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

But at least now I know, and I just have to figure out what's going wrong.
Post Reply