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 7775189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:34:00+00:00 2026-06-01T17:34:00+00:00

I am fetching an array of data from SQL, and then concatenating them as

  • 0

I am fetching an array of data from SQL, and then concatenating them as strings for display. The function looks like this:

function FetchTopStats( Conn, iLimit )
  local sToReturn = "\tS.No. \t UserName \t Score\n\t"
  SQLQuery = assert( Conn:execute( string.format( [[SELECT username, totalcount FROM chatstat ORDER BY totalcount DESC LIMIT %d]], iLimit ) ) )
  DataArray = SQLQuery:fetch ({}, "a")
  i = 1
  while DataArray do
    sToReturn = sToReturn..tostring( i ).."\t"..DataArray.username.." \t "..DataArray.totalcount.."\n\t"
    DataArray = SQLQuery:fetch ({}, "a")
    i = i + 1
  end
  return sToReturn
end

This gives me an output like:

    S.No.    UserName    Score
    1   aim      6641
    2   time     5021
    3   Shepard      4977

and so on. I am thinking of using a string.format function, to have a display as follows:

    S.No.    UserName    Score
    1       aim          6641
    2       time         5021
    3       Shepard      4977

But, I am totally out of ideas on how to have this. The only option coming to my mind is checking string length of username, and then applying \t accordingly. That, I want to use at the very last.

  • 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-01T17:34:01+00:00Added an answer on June 1, 2026 at 5:34 pm

    Well, you need to either find out the maximum length of username thus making the algorithm 2-pass, or limit it to some arbitrary (but reasonable) size and unconditionally chop off the tails of too long strings. Once you have the column width, you can use left or right aligned format strings:

    > print(string.format("|%-10d|%-20s|%10d|", 1, "Shepard", 9000))
    |1         |Shepard             |      9000|
    

    Also, for large tables consider using table.concat for building the final output: it’s considerably faster than repeatedly appending strings (refer to the Chapter 11.6 of PIL for explanation).

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

Sidebar

Related Questions

I am fetching some data from a database like this $sql =SELECT * FROM
In this script, I'm fetching a load of data from a MySQL aray, and
I can't get nl2br function to work after fetching data from my database: $result
I am having one array where i am fetching data from database for some
I had stored custom objects data in Array. I am fetching data from Array
I'm fetching a row of data from a mysql-server using php, then encode it
i am fetching some $_POST vars and put them in an array likes this:
I am doing Ajax Call from View, then controller is fetching data from DB
I am fetching an array and I would like to assign attributes of items
I'm fetching some data from an MSSQL table using the mssql_fetch_object, but the text

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.