2

我有一个获取 5 行数据的查询,如下例所示

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
}

我想在每个结果中运行一个查询,如下所示

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
 $ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}

但由于某种原因,当我在其中添加查询时,它只显示第一行。我似乎可以让它运行所有 5 个查询。

4

8 回答 8

2
SELECT 
    t1.ref, 
    t1.user,
    t1.id,
    t2.domain,
    t2.title
FROM 
    table AS t1
    LEFT JOIN anothertable AS t2 ON
        t2.domain = t1.ref
LIMIT
    0, 5
于 2012-10-08T09:40:35.933 回答
2

问题是在 while 循环中您使用相同的变量$result,然后它会被覆盖。$result在 while 循环中使用另一个变量名。

于 2012-10-08T09:41:26.263 回答
1

您在 while 循环中更改 $query 的值。将变量名称更改为不同的名称。

前任:

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
 $ref = $row['ref'];
$qry = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$rslt = mysql_query($qry) or die(mysql_error());
if (mysql_num_rows($rslt) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
于 2012-10-08T09:40:19.053 回答
1

使用以下内容:

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
    $ref = $row['ref'];
    $query_domain = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
    $result_domain = mysql_query($query_domain) or die(mysql_error());
    if (mysql_num_rows($result_domain) )
    {
        $row_domain = mysql_fetch_row($result_domain);
        $title = $row_domain['title'];
    } else {
        $title = "No Title";
    }
    echo "$ref - $title";
}
于 2012-10-08T09:41:37.280 回答
1

这是一个逻辑问题。它是这样发生的,因为循环内外的变量名相同。

解释:

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
// Now $results hold the result of the first query

while($row = mysql_fetch_array($result))
{
    $ref = $row['ref'];

    //Using same $query does not affect that much
    $query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";    

    //But here you are overriding the previous result set of first query with a new result set
    $result = mysql_query($query) or die(mysql_error());
    //^ Due to this, next time the loop continues, the $result on whose basis it would loop will already be modified

//..............

解决方案1:

避免对内部结果集使用相同的变量名

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
    $ref = $row['ref'];
    $query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";    
    $sub_result = mysql_query($query) or die(mysql_error());
    // ^ Change this variable so that it does not overrides previous result set

解决方案2: 避免双重查询的情况。使用联接在一个查询调用中获取数据。(注意: 您应该始终尝试优化您的查询,以便最大限度地减少服务器上的查询数量。

SELECT 
    ref,user,id 
FROM 
    table t
INNER JOIN 
    anothertable t2 on t.ref t2.domain
LIMIT 0, 5
于 2012-10-08T09:46:17.927 回答
0

了解SQL 连接

SELECT table.ref, table.user, table.id, anothertable.title
FROM   table LEFT JOIN anothertable ON anothertable.domain = table.ref
LIMIT  5
于 2012-10-08T09:40:44.017 回答
0

您正在更改$result循环中的值。更改您的第二个查询以使用不同的变量。

于 2012-10-08T09:41:21.953 回答
0

它没有给出正确的结果,因为您使用了两次相同的名称,使用不同的名称,如此编辑。

$query = "SELECT ref,user,id FROM table LIMIT 0, 5"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
    $ref = $row['ref'];
    $query1 = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
    $result1 = mysql_query($query1) or die(mysql_error());
    if (mysql_num_rows($result1) )
    {
        $title = $row['title'];
    } else {
        $title = "No Title";
    }
    echo "$ref - $tile";
}
于 2012-10-08T09:42:13.417 回答