UserIsCorrupt
UserIsCorrupt

Reputation: 5025

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 )

Upvotes: 0

Views: 177

Answers (3)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48630

$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
}

Upvotes: 1

sakhunzai
sakhunzai

Reputation: 14470

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

 select `Column 1` from tablename;

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

Upvotes: 1

Praveen kalal
Praveen kalal

Reputation: 2129

I think its simple.

select Column 1 From tablename;

Upvotes: 3

Related Questions