如何使用for循环遍历mysql_fetch_array()?
|
我正在用PHP在数据库上执行MySQL
SELECT
,我想遍历结果。我正在用ѭ1来做到这一点。我最初是使用while
循环遍历结果的,我遇到的问题是在循环中我需要获取循环当前所在的行。我认为for循环会这样做,因为那样我就需要$ i来获取问题的价值在于我认为这行不通。下面是我的代码。是否可以按照我的要求去做,而我做对了吗?
$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
$row = mysqli_fetch_array($r, MYSQLI_ASSOC)
for($i=1; i<10; i++) {
$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]\"; //select tags corresponding to this test
$r = mysqli_query ($dbc, $q) or trigger_error(\"Query: $q\\n<br />MySQL Error: \" . mysqli_error($dbc));
}
没有找到相关结果
已邀请:
2 个回复
邦绊门蔽惩
循环,只保留变量
,每次迭代增加一次。
拭十年