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

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

问题描述

我一直在到处寻找这个,但仍然找不到解决方案:如何从 mySQL 列中获取所有值并将它们存储在数组中?

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?

例如:表名称:客户列名:ID、名称行数:5

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

我想得到这个表中所有 5 个名字的数组.我该怎么做?我正在使用 PHP,我只是想:

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

然后使用

mysql_fetch_array

将这些值存储在数组中的 PHP 函数.

PHP function to store those values in an array.

推荐答案

请注意,此答案已过时! 从 PHP7 开始,mysql 扩展不再开箱即用.如果你想在PHP7中使用旧的mysql函数,您必须从 PECL 编译 ext/mysql. 有关更多当前解决方案,请参阅其他答案.

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.

<小时>

这行得通,请在此处查看更多文档: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天全站免登陆