Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

fputcsv with MySQL help needed

Featured Replies

Hi everyone. My aim here is to have a database I have in MySQL write to a csv file when the person visits a link via PHP.

 

So they visit the link -> then the PHP calls the data from MySQL -> that is the written to an array -> which is used to create the csv cells.

 

I have the below so far after reading documentation on PHP.net

 

 

$query = mysql_query("SELECT * FROM my_table", $connection);

$list = array (

while ($row = mysql_fetch_array($query)) {

array(.$row['first-name']., .$row['last-name']., .$row['email'].),

}

array('end', 'end', 'end')

);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
fputcsv($fp, $fields);
}

fclose($fp);

 

I know the above will not work, but i cant seem to figure out how to work around this using the "while" loop to call all the data.

Anyone who can shine some light on a work around would be much appreciated. Thanks in advance.

You can use array_push to put the query results into your array. Something like

$r = mysqli_query($connection, "SELECT * FROM my_table");
$list = array ();
$eof = array('end', 'end', 'end');

while ($row = mysqli_fetch_row($r)) {
array_push($list, $row);
}

array_push($list, $eof);
$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);

 

I've used the mysqli extension which has superceded mysql. You'll need to be using PHP version 5 and MySql version 4 (I think) to use the newer mysqli extension.

  • Author

thanks for the reply, that makes more sense to use array push! although i have tested your method and its only writing "end end end" to my file.

 

So i tried doing:

 

 

while ($row = mysql_fetch_row($r)) {
array_push($list, $row[0]);
array_push($list, $row[1]);
array_push($list, $row[2]);
array_push($list, $row[3]);
}

 

But no luck either, im still trying to look at the problem so will let you know if i find out what it is. Dont suppose you have any further ideas?

  • Author

thanks, your code actually did work, it was just me trying to call data from a table name with a typo in it ha. Thanks for your help

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.