无法从mysql表中获取所有值

时间:2010-02-06 14:37:57

标签: php sql mysql database

我有这张桌子:

option_values table:
option_id (FK)
value_id (PK)
classified_id (FK)
value

示例:

   option_id (FK)     value_id (PK)     classified_id (FK)     value
        1                  1                   22              'Petrol'
        2                  2                   22              'Manual'
        3                  3                   22              'Black'

如何检索和回显使用classified_id = 22找到的每个值?

我试过这个:

  $res=mysql_query("SELECT * FROM option_values WHERE classified_id=22");
  $row = mysql_fetch_row($res);
  echo $row[3]; // This displays 'Petrol'

但是我怎样才能显示'手动','黑'等?

由于

5 个答案:

答案 0 :(得分:3)

使用它,你需要得到一个行数组......他们这样做的方法是在数组中给你一行的值。

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("ID: %s  Value: %s", $row[0], $row[3]);  
}

来源:here

答案 1 :(得分:0)

$res=mysql_query("SELECT * FROM option_values WHERE classified_id=22");
while(FALSE != ($row = mysql_fetch_row($res)) {
    echo $row[3];
}

答案 2 :(得分:0)

 $res=mysql_query("SELECT * FROM option_values WHERE classified_id=22");
  while($row = mysql_fetch_array($res))
  {
    echo $row[1];
    echo $row[2];
    echo $row[3];
  }

答案 3 :(得分:0)

帮自己一个忙,并实现一个像这样的辅助函数:

function fetch_multi_rows($query)
{
    $rows = array();

    while ( $row = mysql_fetch_array($result) )
        $rows[] = $row;

    return $rows;
}

// Use it like this
$my_rows = fetch_multi_rows(...your query...);
foreach ( $my_rows as $row )
    echo $row[3];

干杯!

答案 4 :(得分:0)

您可以编写“更面向对象”的代码,例如::)

// Execute the query
$res = mysql_query("SELECT * FROM option_values WHERE classified_id=22");

// Check for empty result
if (mysql_num_rows($result) == 0) {
    echo "No results.";
    exit;
}

// Fetch results as objects
while ($row = mysql_fetch_object($result)) {
    echo "Option Id: ".$row->option_id;
    echo "Value: ".$row->value;
}

// Free results memory
mysql_free_result($result);


干杯伴侣!