Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

mySQL problem

Options
  • 04-10-2009 2:10pm
    #1
    Registered Users Posts: 3,956 ✭✭✭


    I'm having trouble with a database I'm setting up, notably my php script isn't returning the correct information from my table.

    I'm using the following code:
       $userId = $_SESSION['user'];
       $sql = "SELECT feed, feed_id
               FROM feeds
               WHERE user_id = '$userId' ORDER BY feed_id";
    
       $result = mysql_query($sql)
                 or die('Query failed. ' . mysql_error());
       $feeds = mysql_fetch_assoc($result);
       var_dump($feeds);
    

    This worked fine first run, then I added a new entry to the table through the mysql prompt, but it's not showing up on my page as it should, just the original result is. If I run the query in the mysql prompt I get the correct results. Whats going on here?


Comments

  • Moderators, Music Moderators Posts: 23,359 Mod ✭✭✭✭feylya


    What output are you getting when you var_dump? Check the query that it's running too by adding echo $sql;


  • Registered Users Posts: 3,956 ✭✭✭mp3guy


    Well, the output of echo $sql is:
    SELECT feed, feed_id FROM feeds WHERE user_id = 'john' ORDER BY feed_id;
    

    The output of var_dump is:
    array(2) { ["feed"]=>  string(31) "http://www.rte.ie/rss/sport.xml" ["feed_id"]=>  string(1) "1" }
    

    And then if I run it in the mysql prompt:
    mysql> SELECT feed, feed_id FROM feeds WHERE user_id = 'john' ORDER BY feed_id;
    +-----------------------------------------+---------+
    | feed                                    | feed_id |
    +-----------------------------------------+---------+
    | http://www.rte.ie/rss/sport.xml         |       1 | 
    | http://www.rte.ie/rss/entertainment.xml |       3 | 
    +-----------------------------------------+---------+
    2 rows in set (0.00 sec)
    


  • Moderators, Music Moderators Posts: 23,359 Mod ✭✭✭✭feylya


    mysql_fetch_assoc only returns 1 row at a time ;)

    You need to wrap it in a while loop
    while ($row = mysql_fetch_assoc($result))
    {
    echo $row['feed'] . " - " . $row['feed_id'];
    }
    


  • Registered Users Posts: 3,956 ✭✭✭mp3guy


    Ahh, thanks. That did the trick, I'm new to actually implementing databases, so I may be back with more "queries" :P


  • Moderators, Music Moderators Posts: 23,359 Mod ✭✭✭✭feylya


    No worries. PHP & MySQL is great but you'll start getting some stupidly obvious errors.


  • Advertisement
Advertisement