0

I have an MSSQL query where it SELECTS all rows that meet a specific criteria. In PHP I want to fetch that array, but then I want to convert it into one array per row. So if my query returns 3 rows, I want to have 3 unique arrays that I can work with.

I'm not sure how to go about this. Any help would be greatly appreciated!

Thanks, Nathan


EDIT:

$query = "SELECT * FROM applicants WHERE applicants.user_id ='{$_SESSION['user_id']}'";
$query_select = mssql_query($query , $connection);
if (mssql_num_rows($query_select) == 2){
      $message = '2 students created successfully';
}

$i = 0;
while($row = mssql_fetch_array($query_select)) {
    $student.$i['child_fname'][$i] = $row['child_fname']; 
        $student.$i['child_lname'][$i] = $row['child_lname'];
    $i++;
}
    $query_array1 = $student0;
$query_array2 = $student1;

You will notice from the code above that I am expecting two rows to be returned. Now, I want to take those two rows and create two arrays from the results. I tried using the solution that was give below. Perhaps my syntax is incorrect or I didn't understand how to properly implement his solution. But any help would be greatly appreciated.

Nathan
  • 59
  • 1
  • 14
  • I guess you could [copy the array object](http://stackoverflow.com/questions/1532618/is-there-a-function-make-a-copy-of-a-php-array-to-another) and delete the rows you don't need... but why not just reference the [index of the different rows](http://stackoverflow.com/questions/10760389/accessing-two-dimensional-arrays-php)? – xQbert Mar 12 '13 at 00:27
  • I need all rows returned, but I need to work with them as separate arrays, and not as one big array. – Nathan Mar 12 '13 at 00:30

2 Answers2

0
$i=0;
while( $row = mysql_fetch_array(query){
   $field1['Namefield1'][$i] = $row['Namefield1']; 
   $field2['Namefield2'][$i] = $row['Namefield2']; 
   $field3['Namefield3'][$i] = $row['Namefield3'];
   $i++;
}

or if you preffer an bidimensional array:

$i=0;
while( $row = mysql_fetch_array(query){
   $result['Namefield1'][$i] = $row['Namefield1'];
   $result['Namefield2'][$i] = $row['Namefield2'];
   $result['Namefield3'][$i] = $row['Namefield3'];
   $i++
}
Mollo
  • 723
  • 3
  • 7
  • 24
0
$query = mssql_query('SELECT * FROM mytable');
$result = array();
if (mssql_num_rows($query)) {
    while ($row = mssql_fetch_assoc($query)) {
        $result[] = $row;
    }
}

mssql_free_result($query);

Now you can work with array like you want:

print_r($result[0]); //first row
print_r($result[1]); //second row
...
ole
  • 5,166
  • 5
  • 29
  • 57
  • What did you mean with "I need to work with them as separate arrays"? You need $r1; $r2; ... arrays? – ole Mar 12 '13 at 01:24
  • How would I split assoc. array into individual arrays? – Nathan Mar 12 '13 at 04:48
  • In PHP you can create dynamic variables only as class properties, so if realy need it then you should create class and store results in, create getter method and result will be like: $result->row1; $result->row2; But for what you need this? – ole Mar 12 '13 at 11:59