Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9197169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:00:52+00:00 2026-06-17T22:00:52+00:00

I’m trying to get a script to work wherein certain selected records (depending on

  • 0

I’m trying to get a script to work wherein certain selected records (depending on user input) are selected and then exported to a CSV file. I’ve tried all sorts of methods to do this; thus far this is the most successful I could find (actually from some code I did a few years ago), but it only returns the first row over and over again until the rows run out (you can see my sawdust-in-the-transmission method I used to tell PHP to keep getting rows until the result is exhausted). My questions: There is probably an easier way to do this– any thoughts? If not, what can I do to get MySQL to advance to the next row the next time the loop is run? I thought it was supposed to do that anyway with mysql_fetch_array.

And yes, I’ve looked at fputcsv, but I can’t seem to get any data to pass. The CSV file always comes up blank when I try to pass an array using fputcsv and getting the array via mysql_fetch_array, mysql_fetch_assoc, or mysql_fetch_row.

I’ve been learning as I go, and this is one of those times when I just haven’t learned something in order to keep moving forward. And I don’t know that much, either.

My code, or at least as much as is necessary:

        $result = mysql_query("SELECT id FROM student_history WHERE username='$username'");
    $numrows = mysql_num_rows($result);
    if (!$result) {
        die("Query to show fields from table failed!:" .mysql_error());
    }
    $write = fopen("exportedmagic.csv", 'w') or die ("Can't create/open file to write! Drats!");
    fwrite($write, "ID, Username, Class, Status, Date, Time\n");
    while($numrows > 0) {
        $query = "SELECT id, username, class, status, date, time FROM student_history WHERE username='$username'";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        $id = $row['id'];
        $username = $row['username'];
        $class = $row['class'];
        $status = $row['status'];
        $date = $row['date'];
        $time = $row['time'];
        fwrite($write, $id .", " .$username .", " .$class .", " .$status .", " .$date .", " .$time ."\n");
        $numrows--;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:00:53+00:00Added an answer on June 17, 2026 at 10:00 pm

    Try changing to-

    if($numrows > 0) {
        $query = "SELECT id, username, class, status, date, time FROM student_history WHERE username='$username'";
        $result = mysql_query($query);
        while ($row = mysql_fetch_array($result){
            $id = $row['id'];
            $username = $row['username'];
            $class = $row['class'];
            $status = $row['status'];
            $date = $row['date'];
            $time = $row['time'];
            fwrite($write, $id .", " .$username .", " .$class .", " .$status .", " .$date .", " .$time ."\n");
        }
    }
    

    With using while($numrows > 0){$query= ... $numrows--; you are redoing the query every time you go through the loop.

    Also, instead of using fwrite() you can use fputcsv() which makes it possible to add the row without needing to iterate through each column value in your current code.

    Note: you will want to look at updating to either mysqli_* or PDO as the mysql_* extension is depreciated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need to clean up various Word 'smart' characters in user input, including but
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.