这段代码是否循环MySQL返回的多行?

时间:2011-04-16 23:52:07

标签: php mysql debugging

我有以下PHP代码。它应该从数据库中选择最多十行并获取该信息并将其输入上面定义的数组中,以便稍后在代码中循环并打印在页面上。它应该选择三行,但它只选择最新的一行。知道为什么吗?

//create arrays for storing each tests information
$subject = array();
$tag = array();
$title = array();
$creator = array();
$creation_date = array();
$test_type = array();
$test_id = array();

$q = "SELECT test_id, title, subject, type, creation_date FROM tests WHERE user_id='$user_id' LIMIT 10"; //select first ten of users tests
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

if (mysqli_affected_rows($dbc) > 0) //if the query ran correctly and the test details were gathered from the database
{
    $i = 0;

    while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
    {
        $test_id[] = $row['test_id'];
        $test_type[] = $row['type'];
        $creation_date[] = $row['creation_date'];
        $creator[] = $user_id;
        $title[] = $row['title'];
        $subject[] = $row['subject'];

        $q = "SELECT tag_id FROM test_tags WHERE test_id='$test_id[$i]'"; //selects tags for test
        $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

        if (mysqli_affected_rows($dbc) > 0) //if the query ran correctly and the tag_ids were gathered from the database
        {
            while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
            {
                $thisTag = $row['tag_id'];

                $q = "SELECT name FROM tags WHERE tag_id='$thisTag' LIMIT 1"; //selects tag name
                $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

                if (mysqli_affected_rows($dbc) > 0)//if the query ran correctly and the tags were gathered from the database
                {
                    while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
                    {
                        $tag[$i][] = $row['name'];
                    }
                }
            }
        }
        $i++;
    }
}//end of SELECT if

1 个答案:

答案 0 :(得分:2)

您的行为看起来像是一个多对多的数据库。 (意思是你有很多帖子连接到很多标签) 两件事,您在整个嵌套查询中使用相同的行变量,并且您使用相同的结果变量。两者都应该避免。试试下面的代码,告诉我它是如何工作的。

<?php
//create arrays for storing each tests information
$subject = array();
$tag = array();
$title = array();
$creator = array();
$creation_date = array();
$test_type = array();
$test_id = array();

$q = "SELECT test_id, title, subject, type, creation_date FROM tests WHERE user_id='$user_id' LIMIT 10"; //select first ten of users tests
$r1 = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

if (mysqli_affected_rows($dbc) > 0) //if the query ran correctly and the test details were gathered from the database
{
    $i = 0;

    while($orow = mysqli_fetch_array($r1, MYSQLI_ASSOC))
    {
        $test_id[] = $orow['test_id'];
        $test_type[] = $orow['type'];
        $creation_date[] = $orow['creation_date'];
        $creator[] = $user_id;
        $title[] = $orow['title'];
        $subject[] = $orow['subject'];

        $q = "SELECT tag_id FROM test_tags WHERE test_id='{$test_id[$i]}'"; //selects tags for test
        $r2 = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

        if (mysqli_affected_rows($dbc) > 0) //if the query ran correctly and the tag_ids were gathered from the database
        {
            while($irow = mysqli_fetch_array($r2, MYSQLI_ASSOC))
            {
                $thisTag = $irow['tag_id'];

                $q = "SELECT name FROM tags WHERE tag_id='{$thisTag}' LIMIT 1"; //selects tag name
                $r3 = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

                if (mysqli_affected_rows($dbc) > 0)//if the query ran correctly and the tags were gathered from the database
                {
                    while($iirow = mysqli_fetch_array($r3, MYSQLI_ASSOC))
                    {
                        $tag[$i][] = $iirow['name'];
                    }
                }
            }
        }
        $i++;
    }
}//end of SELECT if
?>