Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

mySQL problem

  • 04-10-2009 02:10PM
    #1
    Registered Users, Registered Users 2 Posts: 3,946 ✭✭✭


    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,363 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, Registered Users 2 Posts: 3,946 ✭✭✭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,363 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, Registered Users 2 Posts: 3,946 ✭✭✭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,363 Mod ✭✭✭✭feylya


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


  • Advertisement
Advertisement