简体   繁体   中英

PHP - Loop inside loop second loop runs only once

I'm running one while inside another while but the second one is running only one time why and how can I fix it. I have also try with for but running again only once.

$sql = "SELECT DISTINCT season FROM search WHERE link = '$getid' Order by id asc";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysql_fetch_assoc($result))
{
    $season = $list['season'];
    $sql = mysql_query("SELECT * FROM search WHERE link = '$getid' and season = '$season'");
    $episodes = mysql_num_rows($sql);
    echo '1st';
    $sqls = "SELECT * FROM search WHERE link = '$getid' and season = '$season' Order by id asc";
    $results = mysql_query($sqls, $conn) or trigger_error("SQL", E_USER_ERROR);
    while ($lists = mysql_fetch_assoc($results))
    {
        $episode = $lists['episode'];
        echo'2nd';
    }
}

You are overriding the variables, use different ones:

$sql = "SELECT DISTINCT season FROM search WHERE link = '$getid' Order by id asc";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysql_fetch_assoc($result))
{
    $season = $list['season'];
    $sql2 = mysql_query("SELECT * FROM search WHERE link = '$getid' and season = '$season'");
    $episodes = mysql_num_rows($sql2);
    echo '1st';
    $sqls = "SELECT * FROM search WHERE link = '$getid' and season = '$season' Order by id asc";
    $results2 = mysql_query($sqls, $conn) or trigger_error("SQL", E_USER_ERROR);
    while ($lists2 = mysql_fetch_assoc($results2))
    {
        $episode = $list2['episode'];
        echo'2nd';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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