3

I have nooby PHP question that I can't figure out!

I loop through rows from my database:

    $data = array();

    while($row = sqlsrv_fetch_array($queryResult, SQLSRV_FETCH_ASSOC)){

        $data[] = $row;
    }

$data now contains an array within an array how can I have it so that its still just a single array?

Thanks all

2
  • an array of what? you may want to index data[] by the database id, right? Commented Jul 1, 2010 at 13:30
  • @galambalazs - its an array of rows from my database. I just want to have it as a single array so that I can process it later more easily. There is no real need but I wanted to know how to do this in case I need to. Commented Jul 1, 2010 at 13:35

4 Answers 4

4

That's because each $row is an associative array. If you just want data to be an array of values from one column, specify that column:

$data = array();
while($row = sqlsrv_fetch_array($queryResult, SQLSRV_FETCH_ASSOC)){
    $data[] = $row['column_name_you_want'];
}
Sign up to request clarification or add additional context in comments.

6 Comments

Awesome, thanks. Its going to be tedious to type all column names but I get the idea behind your method.
Hmmm... What's the point of that? In order to properly get all data, you'll need a 2D array anyways. You'll just be switching the order of the dimensions, but there is no way around having a multidimensional array.
(1) It's an associative array so you'll need to type every column name at least once somewhere to even access the data. (2) Depending on what you need to do with the data, rearranging it may be a waste of time. If you put the rows into $data you can always do something like this $data[0]['column_name'] to access column_name` in the first row, etc.
@Abs the point is you have rows and columns (hence a table): how would you condense both in a simple plain list?
If you did that, @Abs, then you would only ever have the data from the last row in your query.
|
3

It a more obvious way:

$data = array();

while($row = sqlsrv_fetch_array($queryResult, SQLSRV_FETCH_NUMERIC){
  $data = array_merge( $data, array_values($row) );
}

1 Comment

should be while($row = sqlsrv_fetch_array($queryResult, SQLSRV_FETCH_NUMERIC)) {
2

This should get you all values returned from all columns and rows as a single dimension array

$data = array();

while($row = sqlsrv_fetch_array($queryResult, SQLSRV_FETCH_ASSOC)){
    $values = array_values($row);
    foreach($values as $value)
    {
        $data[] = $value;
    }
}

1 Comment

it should be like while($row = sqlsrv_fetch_array($queryResult, SQLSRV_FETCH_ASSOC)) {
0

I see this is an old question, but I was also looking for a solution to this. Came up with the following which works for me:

function mssql_query($conn, $query, array $bind = array()) {
    $stmt = sqlsrv_query($conn, $query, $bind);
    if( $stmt=== false ) {
        // do something with the error information
        // die(print_r(sqlsrv_errors(), true));
        return array();
    }
    sqlsrv_execute($stmt);
    $data = array();
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        array_push($data, $row);
    }
    return $data;
}

Which can be used directly in a foreach loop:

foreach(mssql_query($conn, $query) as $row) {
    echo $row['column'];
}

Or assigned to a variable for counting etc.

$conn is of course the connection (from PHP.net):

$serverName = "server.example.com"; // remember to append instance if applicable
$connectionInfo = array( "Database"=>"dbname", "UID"=>"username", "PWD"=>"password" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
}

Comments

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.