当我尝试mysql_fetch_row
时,创建的数组包含我在每个索引处选择的2个字段。我想问为什么会这样?
<?php
$categoryid = $_GET['id'];
include('connect.php');
$query = "SELECT
Categories_SubCategories.IdCategory,Categories_SubCategories.idSub_Category,
Categories.Name, Sub_Categories.Name from Categories_SubCategories JOIN
Categories on Categories.idCategory = Categories_SubCategories.idCategory
JOIN Sub_Categories on Categories_SubCategories.idSub_Category =
Sub_Categories.idSub_Category
WHERE Categories_SubCategories.IdCategory = $categoryid";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
for($i=0; $i<$rows; $i++){
$display = mysql_fetch_row($result);
echo "$display[3]";
}
?>
答案 0 :(得分:0)
将mysql_num_rows
替换为mysql_fetch_array
或mysql_fetch_assoc
答案 1 :(得分:0)
大多数PHP程序员在想要使用while()
时都会使用mysql_fetch_array()
循环。
看看这个示例代码:
$query = mysql_query("SELECT id,name FROM tbl_members");
if (mysql_num_rows($query)) {
while ($result = mysql_fetch_array($query)) {
echo('User #'.$result['id'].' is: '.$result['name'].'<br />');
}
}
// Output can be something like this:
// User #1 is: John
// User #2 is: Sarah