如何获得使用PHP列的所有值? [英] How get all values in a column using PHP?

查看:111
本文介绍了如何获得使用PHP列的所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这无处不在,但仍然无法找到一个解决方案:怎样让所有的值从MySQL列,并将它们存储在一个数组

有关如:
 表名称:客户
 列名:ID,名称
 行#:5

我要得到所有此表中的5名组成的数组。我该如何去这样做呢?我使用PHP和我试图只是:

  SELECT名客户

,然后用

  mysql_fetch_array

PHP函数的值存储在数组中。

在这个任何想法?

在此先感谢!


解决方案

  

请注意,这个答案已经过时了! MySQL扩展不再提供开箱即用的PHP7的。如果要使用旧版本的MySQL功能PHP7,的你将不得不从PECL编译分机/ MySQL的。见其他答案更多的现有解决方案。



这会工作,在这里看到更多的文档:
http://php.net/manual/en/function.mysql-fetch -array.php

  $结果= mysql_query(选择客户名称);
$ storeArray =阵列();
而($行= mysql_fetch_array($结果,MYSQL_ASSOC)){
    $ storeArray [] = $行['姓名'];
}
//现在$ storeArray将拥有所有的名字。

I've been searching for this everywhere, but still can't find a solution: How do I get all the values from a mySQL column and store them in an array?

For eg: Table Name: Customers Column names: ID, Name # of rows: 5

I want to get an array of all the 5 names in this table. How do I go about doing that? I am using PHP, and I was trying to just:

SELECT names FROM Customers

and then use the

mysql_fetch_array

PHP function to store those values in an array.

Any ideas on this?

Thanks in advance!

解决方案

Note that this answer is outdated! The mysql extension is no longer available out of the box as of PHP7. If you want to use the old mysql functions in PHP7, you will have to compile ext/mysql from PECL. See the other answers for more current solutions.


This would work, see more documentation here : http://php.net/manual/en/function.mysql-fetch-array.php

$result = mysql_query("SELECT names FROM Customers");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $storeArray[] =  $row['names'];  
}
// now $storeArray will have all the names.

这篇关于如何获得使用PHP列的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆