fri3ndly Posted January 2, 2008 Share Posted January 2, 2008 Hi everyone, Im fairly new to this but know the basics. I have a script I am working on and I am trying to pull the following information from a database table: $data = mysql_query("SELECT * FROM downloads WHERE username='$u'") It is working, button is only pulling the data from one row, but not every row where the user = $user Please can someone help me out, can supply further details/files Many Thanks, and happy new year!! Link to comment Share on other sites More sharing options...
UltraFlynn Posted January 2, 2008 Share Posted January 2, 2008 Can you post the code you are using to iterate over the $data collection? Link to comment Share on other sites More sharing options...
fri3ndly Posted January 2, 2008 Author Share Posted January 2, 2008 $data = mysql_query("SELECT * FROM downloads WHERE username='$u'") or die(mysql_error()); $info = mysql_fetch_array( $data ); if ($info['file_id']>=1){ Print "</pre><table id="filetable">"; Print " File Name Date Added Download File "; Print " ".$info['filename'] . " ".$info['dateadded'] . " ".$info['download'] . " "; Print "</table>"; <br> <br> }<br> <br> else echo "There are currently no files available for you to download.";<br Thanks Link to comment Share on other sites More sharing options...
UltraFlynn Posted January 2, 2008 Share Posted January 2, 2008 Unless I'm missing something, there's no loop there so you'll only get the first row of data. Surely there should be a loop around this bit: Print " ".$info['filename'] . " ".$info['dateadded'] . " ".$info['download'] . " "; And you'd need this in there as well to pull the next row from the result set: $info = mysql_fetch_array( $data ); Link to comment Share on other sites More sharing options...
fri3ndly Posted January 2, 2008 Author Share Posted January 2, 2008 Oh right. Its just that the same code worked on a script I did before and pulled everything from the table, but this doesnt. How would I go about putting a loop in there? Thanks for your help Edit: I am looking at this page (http://www.killerphp.com/videos/11_loops/loops.php) and will post up if I have any more trouble, thanks a lot Link to comment Share on other sites More sharing options...
UltraFlynn Posted January 2, 2008 Share Posted January 2, 2008 Here's an example of some code which iterates over a result set: while ($row = mysql_fetch_array($data)) { $Surname = $row['Surname']; print "" . $Surname . ""; } I've not run this so it might need some work. You might like to think about renaming $data to be $query because it makes more sense. Link to comment Share on other sites More sharing options...
fri3ndly Posted January 2, 2008 Author Share Posted January 2, 2008 Hi Flynn, thanks for all your help. I have got it working now which I am pleased about, however the first row does not display, only the rows thereafter. $query = mysql_query("SELECT * FROM downloads WHERE username='$u'") or die(mysql_error()); $result = mysql_fetch_array( $query ); if ($result['file_id']>=1){ Print "</pre><table id="filetable">"; Print " File Name Date Added Download File "; while ($row = mysql_fetch_array($query)) { $filename = $row['filename']; $dateadded = $row['dateadded']; $download = $row['download']; print ""; print "" . $filename . ""; print "" . $dateadded . ""; print "" . $download . ""; } Print " </table><br> "; <br> <br>}<br> <br>else echo "There are currently no files available for you to download.";<br><br I have googled it and can see that the first row is actually being initiated in an IF statement so the script assumes it has already processed it. I think I need to use 'mysql_num_rows' for the IF statement instead, but just trying to work out how to use it Cheers again matey! Link to comment Share on other sites More sharing options...
UltraFlynn Posted January 2, 2008 Share Posted January 2, 2008 You're missing the first row because you call the following twice: $result = mysql_fetch_array( $query ); If you remove the first one it'll process the first row as expected. That will mess up your "if ($result['file_id']>=1){" though. What exactly is that trying to achieve? Link to comment Share on other sites More sharing options...
fri3ndly Posted January 2, 2008 Author Share Posted January 2, 2008 SORTED IT, Thanks mate! Link to comment Share on other sites More sharing options...
UltraFlynn Posted January 2, 2008 Share Posted January 2, 2008 Use $query.mysql_num_rows() to determine the number of rows in your result set instead. Also, you might want to add the following to the end to free up resources: mysql_free_result($result); Link to comment Share on other sites More sharing options...
UltraFlynn Posted January 2, 2008 Share Posted January 2, 2008 SORTED IT, Thanks mate! A pleasure. A good place for programming help is here. Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now