SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [PHP Noob] Pulling blog posts out of a database?

    Hey,
    I'm having trouble trying to pull out blog posts from a database..

    I'm following this site, but it doesnt seem to want to do what I want, from looking at the code anyway..

    What I want it to do is like a wordpress blog, it will pull out the title for the post, and then a select number of letters, say, 250, of the main text, then move on to the next one...

    Any advice?

  2. #2
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Advocation View Post
    Hey,
    I'm having trouble trying to pull out blog posts from a database..

    I'm following this site, but it doesnt seem to want to do what I want, from looking at the code anyway..

    What I want it to do is like a wordpress blog, it will pull out the title for the post, and then a select number of letters, say, 250, of the main text, then move on to the next one...

    Any advice?
    Im guessing what you really want is a mysql query.
    Code:
    SELECT title, LEFT(text, 250) FROM blog
    PHP | MySQL | (X)HTML | CSS

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but how do I select it so that after the first post that it gets from the database, it moves onto the next one?

    Like in ASP it would be:

    Posts = databaseconnection.execute (SELECT title, LEFT(content, 250) FROM blog)
    do while posts.EOF = False
    Response.Write "post details"
    posts.movenext
    Loop

    And it would loop through the query results posting the data out and then moving onto the next record...

  4. #4
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    mysql_connect("HOST""USER""PASSWORD") or die(mysql_error());
    mysql_select_db("DATABASE") or die(mysql_error());
    $result mysql_query("SELECT title, LEFT(text, 250) FROM blog");

    while(
    $row mysql_fetch_array$result )) {
        echo 
    $row['title']."<br><br>".$row['text'];

    That should work.
    PHP | MySQL | (X)HTML | CSS

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What i'm using doesnt work...

    When I echo out the SQL query it just comes up with the following: Resource id#3

    Here is the code i'm using:
    PHP Code:
    $result mysql_query("SELECT id, title, content FROM tblPosts WHERE datedeleted IS NULL ORDER BY id");
    echo 
    $result;
    while(
    $row mysql_fetch_array($result)){
         echo 
    $row['title']."<div class=\"post\">".$row['content'];


  6. #6
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You cannot echo "$result" as it is a resource, not a string.

    So what is the actual outcome?
    PHP | MySQL | (X)HTML | CSS

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    noting... nothing happens..

    it just goes onto the html that I have after the php tags..

  8. #8
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try

    mysql_fetch_assoc

    instead of

    mysql_fetch_array
    PHP | MySQL | (X)HTML | CSS

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope, no effect..

  10. #10
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm... I have no idea... is <div class="post"> being shown in the HTML source anywhere? If not, its probably a problem with the query.

    This thread reminds me of how long its been since I didn't use a class for my MySQL queries...
    PHP | MySQL | (X)HTML | CSS

  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm.. ok, i've just removed the "WHERE datedeleted IS NULL ORDER BY id" bit from the sql query and it works...

  12. #12
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem with the query then. Try

    "WHERE datedeleted='' ORDER BY id"
    PHP | MySQL | (X)HTML | CSS

  13. #13
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brent5392 View Post
    Problem with the query then. Try

    "WHERE datedeleted='' ORDER BY id"
    Yep, that works Thanks very much

  14. #14
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anytime, thats what im here for.
    PHP | MySQL | (X)HTML | CSS


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
  •