PHP:过滤特定列的mysql_query结果?

时间:2013-02-15 04:26:32

标签: php mysql filter

是否有一种快速方法可以过滤mysql_query结果以获取仅包含特定列值的列表?

$query = mysql_query("SELECT * FROM users");
$list_of_user_names = get_values($query,"names");

代替get_values使用的函数的名称是什么?

3 个答案:

答案 0 :(得分:1)

假设您在数据库中的字段名称是“名称”

$query = mysql_query("SELECT names FROM users");

while($row = mysql_fetch_assoc($query)){
    echo $row['names'];
    echo "<br>";
}

注意:在新版本的php中不推荐使用mysql_ *函数,使用mysqli *或PDO

答案 1 :(得分:0)

使用以下功能。

  function get_values($q,$c)
  {
       $arr = array();
       while($row = mysql_fetch_array($q))
       {
            $arr[] = $row[$c];
       }
       return $arr; // return all names value.
  }

答案 2 :(得分:0)

试试这个:

$query = mysql_query("SELECT names FROM users");

if (!$query) {
    echo "Could not successfully run query from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($query) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $names

while ($row = mysql_fetch_assoc($query)) {
    echo $row["names"];

}