开发者

Select statement within a while statement

开发者 https://www.devze.com 2023-03-19 12:58 出处:网络
First, I coded this, which looks inside a table, gets the last 10 entries, and displays them. The output is as expected, a list of the 10 last entries in the database.

First, I coded this, which looks inside a table, gets the last 10 entries, and displays them. The output is as expected, a list of the 10 last entries in the database.

$query = "SELECT dfid FROM downloads_downloads ORDER BY did DESC limit 10"; 
$dlresult = mysql_query( $query );
$i=0;
$num = mysql_num_rows ($dlresult);

     while ($i < $num) {
          $dfid= mysql_result($dlresult,$i,"dfid");

          echo "<b>filenumber:</b> $dfid <br>";

          ++$i; 
                  }

But I don't just need the filenumber. I need the actual filename and url from another table. So I added a select statement inside the while statement, using the file number.

But for some reason, this code only displays one filename instead of 10. I know, from the above code, it's getting all 10 file numbers开发者_Go百科.

$query = "SELECT dfid FROM downloads_downloads ORDER BY did DESC limit 10"; 
$dlresult = mysql_query( $query );
$i=0;
$num = mysql_num_rows ($dlresult);

     while ($i < $num) {
         $dfid= mysql_result($dlresult,$i,"dfid");
         $query2 = "SELECT file_name, file_name_furl FROM downloads_files WHERE file_id = '$dfid'";
         $dlresult2 = mysql_query( $query2 );
         $dlfile_name= mysql_result($dlresult2,$i,"file_name");
         $dlfile_name_furl= mysql_result($dlresult2,$i,"file_name_furl");

         echo "filenumber: $dfid <br>"; //Shows 10, as expected.
         echo "filename: $dlfile_name - $dlfile_name_furl <br>"; //Shows only 1?

         ++$i; 
                  }

I can manually execute the sql statement and retrieve the file_name and file_name_furl from the table. So the data is right. PHP isn't liking the select within the while statement?


Looks like you're only going to ever have 1 row, in your 2nd select statement, because you are just selecting one row, with your where statement.

So, you're only going to ever have row 0 in the 2nd statement, so its only finding row 0 on the first loop. try instead:

$dlfile_name= mysql_result($dlresult2,0,"file_name");
$dlfile_name_furl= mysql_result($dlresult2,0,"file_name_furl");

However, insrtead of making 11 separate queries, try using just one:

$link = new mysqli(1,2,3,4);

$query = "SELECT downloads_downloads.dfid, downloads_files.file_name, downloads_files.file_name_furl FROM downloads_downloads LEFT OUTER JOIN downloads_files ON downloads_files.file_id = downloads_downloads.dfid ORDER BY downloads_downloads.dfid DESC limit 10;

$result = $link->query($query) ;
if((isset($result->num_rows)) && ($result->num_rows != '')) {
while ($row = $result->fetch_assoc()) {
    echo "filenumber: $row['dfid'] <br>"; 
    echo "filename: $row['file_name'] - $row['file_name_furl'] <br>"; 
}

Read up on mysql joins http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php

I'm not sure if this is syntax correct, but it gives you the right idea =)

0

精彩评论

暂无评论...
验证码 换一张
取 消