简体   繁体   中英

Return certain column from every row in a MySQL table in an array in PHP

So let's say this is the hierarchy of my database in MySQL.

Database: Example
  Table: Test
    Row 1
      Column 1 ("one"): Whatever
      Column 2 ("two"): Something
      Column 3 ("three"): Test
    Row 2
      Column 1  ("one"): Blah
      Column 2  ("two"): Testing
      Column 3  ("three"): Yup

How can I return an array that has the values of the one columns?

The array would look like this:

Array ( [0] => Whatever [1] => Blah )

我认为这很简单。

select Column 1 From tablename;

Might be issue with the spaces. If there are spaces in column name use back quotes eg

 select `Column 1` from tablename;

I am not sure your example depicts your actual problem :(

$query = "SELECT column1 FROM test";
$result = mysql_query($query);
$numRows = mysql_num_rows($result); // should be 2

while($row = mysql_fetch_array($result)) {
  $row[0]; // data from column
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM