Skip to content
Advertisement

SQL to .csv – How to Get Rid of Double Array Entries

I’m using php code to get the values from a database table with 3 columns and put them into a .csv file

The problem is that I’m always getting double entries of every column. Here is the first row that shows the problem. The array should be 3 items but it is 6.

array(6) { [0]=> string(2) "99" ["id"]=> string(2) "99" [1]=> int(9) ["survey_id"]=> int(9) [2]=> string(10) "Very Small" ["item"]=> string(10) "Very Small" } 

Here is how I’m getting the above data

  $sql="Select * from Percept_Segments";
  $stmt = sqlsrv_query( $conn, $sql);
  while($row = sqlsrv_fetch_array($stmt)){
    var_dump($row);
    exit();

  }

I’ve tried using array_values but this gives the same result. Is there a way to just get the 3 entries like in the original database table?

Advertisement

Answer

Set the fetchType of sqlsrv_fetch_array like this:

sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)

By default according to the Documentation it sets the return array with both associative and numeric.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement