0

I am trying to get all the results from 1 column into an array. I tried the following below but all I get from the output is "Array".

$sql = "SELECT pickID FROM picks WHERE userID = 2 ";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);

$i = 0;
$pickArray = array();

foreach($result as $row) {
    $pickArray[$i] = $row['pickID'];
    $i++;
}

echo $pickArray;
3
  • 2
    try print_r($pickArray) instead of echo. Commented Aug 12, 2014 at 23:51
  • 2
    note- your current code will only return 1 value as mysql_fetch_array() ->Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. If more than 1 value is desired, remove $result = mysql_fetch_array($query); and change foreach($result as $row) { to while($row = mysql_fetch_array($query)){ Commented Aug 12, 2014 at 23:54
  • Thanks Sean for pointing that out. Commented Aug 12, 2014 at 23:59

1 Answer 1

4

You need to use print_r not echo. See why you need to use print_r for viewing values of an array. Echo on the other hand is being used to display value of a variable not array.

print_r($pickArray);

Also a note from Sean,

remove $result = mysql_fetch_array($query); and change foreach($result as $row) { to while($row = mysql_fetch_array($query)){

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer and sean's answer did the job
See my Edit using Sean's suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.