1

当我尝试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]";
  }
?>
4

2 回答 2

0

替换mysql_num_rowsmysql_fetch_arraymysql_fetch_assoc

于 2012-05-21T16:09:10.360 回答
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
于 2012-05-21T16:17:28.000 回答