Does this code loop through multiple rows returned by MySQL?
I have the PHP code below. It is supposed to select up to ten rows from the database and take that information and input it in arrays defined above to be looped through later on in the code and printed on the page. There are three rows it is supposed to select but it is only selecting the latest one. Any idea why?
//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
What it looks like your doing is a many to many database. (meaning you have many posts connected to many tags).
Two things, you are using the same row variable throughout the nested queries, and you are using the same result variable. both should be avoided. Try the code below, and tell me how it works.
<?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
?>
精彩评论