themerlinproject themerlinproject - 1 month ago 8
PHP Question

mysql select single column as single dimension array in PHP

I'm selecting a single column from my mysql database:

$savedSQL = 'SELECT can_id FROM savedsearches WHERE user_id = "'.mysql_real_escape_string($user_id).'" AND insertTime >= "'.$lastSigTime.'"';
$savedQuery = mysql_query($savedSQL);


And I'd like to return the values as a single dimension, enumerated array such that array[0] = row1, array[1] = row2, etc.

When I put it into an array like this:

while($savedResult = mysql_fetch_array($savedQuery))
{ $savedArray[] = $savedResult; }


It returns it as a multi-dimension array so that array[0][0] = row1, array[1][0] = row2, etc.

I was thinking of adding something like this:

while($i=0;$i<count($savedArray);$i++)
{
$newSavedArray[$i] = $savedArray[$i][0]
}


But is there an easier, more efficient way to accomplish this?

Answer

You're very close. You just need to access $savedResult[0] to retrieve your column, and append it onto $savedArray

while($savedResult = mysql_fetch_array($savedQuery)) {
  $savedArray[] = $savedResult[0];
}