Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 这段代码是否遍历MySQL返回的多行?_Php_Mysql_Debugging - Fatal编程技术网

Php 这段代码是否遍历MySQL返回的多行?

Php 这段代码是否遍历MySQL返回的多行?,php,mysql,debugging,Php,Mysql,Debugging,我有下面的PHP代码。它应该从数据库中选择最多十行,并获取该信息,然后将其输入到上面定义的数组中,稍后在代码中循环并打印在页面上。它应该选择三行,但只选择最新的一行。知道为什么吗 //create arrays for storing each tests information $subject = array(); $tag = array(); $title = array(); $creator = array(); $creation_date = array(); $test_typ

我有下面的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
//创建用于存储每个测试信息的数组
$subject=array();
$tag=array();
$title=array();
$creator=array();
$creation_date=array();
$test_type=array();
$test_id=array();
$q=“从测试中选择测试id、标题、主题、类型、创建日期,其中用户id=“$user\u id”限制为10”//选择前十个用户测试
$r=mysqli_query($dbc,$q)或trigger_error(“query:$q\n
MySQL error:“.mysqli_error($dbc)); if(mysqli_impacted_rows($dbc)>0)//查询是否正确运行,测试详细信息是否从数据库中收集 { $i=0; 而($row=mysqli\u fetch\u数组($r,mysqli\u ASSOC)) { $test_id[]=$row['test_id']; $test_type[]=$row['type']; $creation_date[]=$row['creation_date']; $creator[]=$user\u id; $title[]=$row['title']; $subject[]=$row['subject']; $q=“从测试标签中选择标签id,其中测试id='$test\U id[$i]'”;//为测试选择标签 $r=mysqli_query($dbc,$q)或trigger_error(“query:$q\n
MySQL error:“.mysqli_error($dbc)); if(mysqli_impacted_rows($dbc)>0)//查询是否正确运行,标记ID是否从数据库中收集 { 而($row=mysqli\u fetch\u数组($r,mysqli\u ASSOC)) { $thisTag=$row['tag_id']; $q=“从标记中选择名称,其中标记\u id='$thisTag'限制1'//选择标记名称 $r=mysqli_query($dbc,$q)或trigger_error(“query:$q\n
MySQL error:“.mysqli_error($dbc)); if(mysqli_impacted_rows($dbc)>0)//查询是否正确运行,标记是否从数据库中收集 { 而($row=mysqli\u fetch\u数组($r,mysqli\u ASSOC)) { $tag[$i][]=$row['name']; } } } } $i++; } }//结束选择如果
看起来您所做的是一个多对多数据库。(意味着您有许多帖子连接到许多标签)。
两件事是,在整个嵌套查询中使用相同的行变量,并且使用相同的结果变量。两者都应该避免。请尝试下面的代码,并告诉我它是如何工作的

<?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
?>


适当缩进您的代码或半身像。已修复,等待同行审阅。您确定选择了所需的3行吗?如果在phpMyAdmin上运行相同的查询(例如),它是否返回您希望它返回的内容?我这么问是因为人们经常在别的地方寻找问题。@coreyward是的,很抱歉。我真的需要养成这样做的习惯@非常感谢@弗兰基是的,我做了,很好。是的,非常感谢!这正是它所需要的!