Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    51
    Thanks
    2
    Thanked 0 Times in 0 Posts

    getting all of an array from mysql

    I've had no formal training in PHP/MySQL, just what I pick up from my books. So, as many others have done, I am creating a simple front page where it shows news with a date greater than or equal to today's. The problem is, I always lose one news item (somehow due to my arrays), but I don't know enough about the language to understand why.
    So, I've got my connection stuff, then this:
    Code:
    $sql = "SELECT content  FROM `news` WHERE `date` >= '2007-01-28' ORDER BY date";
    $rs = mysql_query($sql, $conn);
    $foo = mysql_fetch_array($rs);
    if($foo) {
    $row = $foo;
    }
    Note that I still have a permanent date, because I've been working on it over a while and I don't want to have to change the dates in my db to be in the future.
    Then, down later, I've got:
    Code:
    while ( $row = mysql_fetch_array($rs) )
    {
      echo ($row["content"] . "<br>");
    }
    As I said, I always lose the 1st result, but am not sure quite why. Thanks for the help.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Your first result you fetched on the third line of the first block of code you posted, here:

    PHP Code:
    $foo mysql_fetch_array($rs);  //fetches first row in the query result 
    Then you don't do anything with that row before you begin your loop, and the first thing you do is fetch another row:

    PHP Code:
    while ( $row mysql_fetch_array($rs) ) //fetches the next row in the query result
    {
      echo (
    $row["content"] . "<br>");

    Plus, you're checking the data you assigned to the fetched row, when you should be checking the mysql_query() result:
    PHP Code:
    $rs mysql_query($sql$conn);
    // $foo = mysql_fetch_array($rs);  //just don't do this here
    //if($foo) {
    if (!rs) {  //check the result of mysql_query()
        
    die("SQL Error! ".mysql_error());


  • #3
    New Coder
    Join Date
    Apr 2005
    Posts
    51
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you very much. That was what I was looking for.


  •  

    Posting Permissions

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