Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  $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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading