从 MySQL 列创建 PHP 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1538229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:00:39  来源:igfitidea点击:

Create PHP array from MySQL column

phpmysqlarrays

提问by Will Peavy

mysql_fetch_arraywill give me an array of a fetched row. What's the best way generate an array from the values of all rows in one column?

mysql_fetch_array会给我一个获取行的数组。从一列中所有行的值生成数组的最佳方法是什么?

Edit: Lots of great answers. Thank you everyone who replied! I ended up going with a solution similar to what Gumbo and GSto suggested. I gave the answer points to GSto because he has less points (just helping out the little guy, nothing personal).

编辑:很多很棒的答案。谢谢所有回复的人!我最终采用了类似于 Gumbo 和 GSto 建议的解决方案。我给了 GSto 的答案分数,因为他的分数较少(只是帮助小家伙,没有什么私人的)。

回答by GSto

you could loop through the array, and create a new one, like so:

您可以遍历数组,然后创建一个新数组,如下所示:

$column = array();

while($row = mysql_fetch_array($info)){
    $column[] = $row[$key];
//Edited - added semicolon at the End of line.1st and 4th(prev) line

}

回答by Tom Haigh

There is no function to do this using the mysql extension, you can do this:

没有使用 mysql 扩展执行此操作的功能,您可以这样做:

$result = array();
while ($row = mysql_fetch_array($r, MYSQL_NUM)) {
    $result[] = $row[0];
}

It is apparently marginally faster to fetch the columns in a numerically indexed array, and there is no real benefit here to having the associative array format.

获取数字索引数组中的列显然要快一些,并且使用关联数组格式在这里没有真正的好处。

回答by Gumbo

Use a whileloop to get the records and store them in an array:

使用while循环获取记录并将它们存储在数组中:

$array = array();
while ($row = mysql_fetch_array()) {
    $array[] = $row['column-x'];
}

回答by Dominic Rodger

Loop through the result:

循环遍历结果:

$result = mysql_query(...);
$data = array();
while ($row = mysql_fetch_array($result)) {
    array_push($data, $row["columnyouwant"]);
}

回答by code_burgar

$result = mysql_query("SELECT columnname FROM table WHERE x=y");

$columnValues = Array();

while ( $row = mysql_fetch_assoc($result) ) {

  $columnValues[] = $row['columnname'];

}

回答by Royendgel Silberie

// I'm using this for years and it works very good !! it's easy and logic
$translate = array();

while ($row = mysql_fetch_assoc($result))
  {
      $cullomnkey = $row['translshort']; // is what you want the key to be 
      $translate[$cullomnkey] = $row[$language]; // this is the key and the value in the array 
  }

回答by VolkerK

If you use PDOinstead of php-mysql the return value of PDO::query() implements the Traversable interface, meaning you can use it e.g. with foreach() (unlike the mysql result resource you get from mysql_query).

如果您使用PDO而不是 php-mysql,则 PDO::query() 的返回值实现了 Traversable 接口,这意味着您可以将它与 foreach() 一起使用(与您从 mysql_query 获得的 mysql 结果资源不同)。

foreach( $pdo->query('SELECT x,y,z FROM foo') as $row ) {

And in case this does not suffice and you really, really need an array (i.e. get_class($x)==='Array'), there's the fetchAll()method.

如果这还不够,而您确实需要一个数组(即 get_class($x)==='Array'),则可以使用fetchAll()方法。

回答by Faron

$query = mysql_query('SELECT * from yourTable');
function mysql_field_array( $query ) {
    $field = mysql_num_fields( $query );
    for ( $i = 0; $i < $field; $i++ ) {
        $names[] = mysql_field_name( $query, $i );
    }
    return $names;
}

$fields = mysql_field_array( $query );
$output = implode( ',', $fields );   //outputs the columns names
//echo count( $fields );  //this use if you want count of columns.
$columns = '{\"fields\":\".json_encode($output).\"}';
echo $columns; //for JSON output

回答by user2334541

you can do this :

你可以这样做 :

        $columns = array();
        $i=1;
        while( $row = mysql_fetch_array($sql) )
           {
              $columns [$i]=$row['value'];
              $i++;
           }

回答by quasi

If you don't need other information in that table, you can query for only the column you need and it makes it all more easy:

如果您不需要该表中的其他信息,您可以只查询您需要的列,这样就更容易了:

$query = mysql_query("SELECT * FROM table WHERE id='$int' LIMIT 1");
$column = array();
$column  = mysql_fetch_array($query);