Data not being inserted into CSV from PHP - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Tuesday, June 21, 2022

Data not being inserted into CSV from PHP

I am trying to insert the results of a mySQL query to select the personality column entries and the number of rows they each appear in. However, the rows are empty, even if the query returns something.

$fields = array('PERSONALITY', 'NUM_AUTOGRAPHS');
    fputcsv($f, $fields, $delimiter);

$personalities = $db->query("SELECT personality ,COUNT(*) FROM autographs GROUP BY personality ORDER BY COUNT(*) DESC");

    while ($row = $query->fetch_assoc()) {
        $lineData = array($row['personality'], $row['count']);
        fputcsv($f, $lineData, $delimiter);
    }

I have two entries in the personalities table, so I would expect two rows in the CSV file, with the right column being set to 1. However, nothing appears.

I have also tried fetch_array() and build the array as array($row[0], $row[1]) but to no avail.

I have other data inserted into the CSV above and everything is fine, so it's not a problem with generating the CSV itself. The column names appear properly, it's just the rows below which are empty.

What could be the cause? Also, I am aware that what I'm doing is very prone to SQL injection but this is not my problem at this time.

Thank you in advance!



source https://stackoverflow.com/questions/72691694/data-not-being-inserted-into-csv-from-php

No comments:

Post a Comment