简体   繁体   English

Select 语句内的 while 语句

[英]Select statement within a while statement

First, I coded this, which looks inside a table, gets the last 10 entries, and displays them.首先,我对此进行了编码,它查看表格内部,获取最后 10 个条目并显示它们。 The output is as expected, a list of the 10 last entries in the database. output 符合预期,是数据库中最后 10 个条目的列表。

$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.我需要另一个表中的实际文件名和 url。 So I added a select statement inside the while statement, using the file number.所以我在while语句中添加了一个select语句,使用文件号。

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.但是由于某种原因,这段代码只显示一个文件名而不是 10 个。我知道,从上面的代码中,它得到了所有 10 个文件号。

$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.我可以手动执行 sql 语句并从表中检索 file_name 和 file_name_furl。 So the data is right.所以数据是对的。 PHP isn't liking the select within the while statement? PHP 不喜欢 while 语句中的 select 吗?

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.看起来你只会有 1 行,在你的第二个 select 语句中,因为你只是用你的 where 语句选择了一行。

So, you're only going to ever have row 0 in the 2nd statement, so its only finding row 0 on the first loop.因此,您只会在第二条语句中拥有第 0 行,因此它只会在第一个循环中找到第 0 行。 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:但是,不要进行 11 个单独的查询,而是尝试只使用一个:

$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阅读 mysql 加入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 =)我不确定这是否语法正确,但它给了你正确的想法 =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM