Jump to content
The mkiv Supra Owners Club

Who knows their PHP/MYSQL?


fri3ndly

Recommended Posts

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

$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

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

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

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

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

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. You might also be interested in our Guidelines, Privacy Policy and Terms of Use.