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

  • Home
  • SEARCH
  • 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 8997535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:54:09+00:00 2026-06-15T23:54:09+00:00

This is the ugliest thing I’ve ever written, but I just don’t know a

  • 0

This is the ugliest thing I’ve ever written, but I just don’t know a way to do this otherwise.
The issue is that I need to submit an SQL Query to a database that relies on information extracted from another location using iteration.

EG: I can extract the data with nested for loops, but then need to put an entire row back together to make an insert query for SQL. I tried temporarily storing the cell data in variables and doing it that way, but that also looked awful and didn’t work.

Here is the code I am using now:

Please don’t get mad. I know it’s awful. I want to be better.
On a side note, I’ve taken to calling this line (line 98) the kiloline, because it’s over 1000 characters long.

$res1 = pg_query("INSERT INTO Project_Time_Sheet VALUES ('" . 
    $objWorksheet->getCellByColumnAndRow(0, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(1, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(2, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(3, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(4, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(5, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(6, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(7, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(8, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(9, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(10, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(11, $row)->getFormattedValue() . "', '" . 
    $objWorksheet->getCellByColumnAndRow(12, $row)->getFormattedValue() . "')");

Please show me a better/more elegant/not asinine way to do this.

  • 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-15T23:54:10+00:00Added an answer on June 15, 2026 at 11:54 pm

    So, I’m going to try directly improving the assembly of this statement. This might not completely answer your question, but it should at least push you down the right path.

    $res1 = pg_query("INSERT INTO Project_Time_Sheet VALUES ('" . $objWorksheet->getCellByColumnAndRow(0, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(1, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(2, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(3, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(4, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(5, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(6, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(7, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(8, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(9, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(10, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(11, $row)->getFormattedValue() . "', '" 
        . $objWorksheet->getCellByColumnAndRow(12, $row)->getFormattedValue() . "')");
    

    Now, doesn’t that look better? No? Slightly more horrifying? Sorry.

    Assumption: You’ll always call getCellByColumnAndRow with a number as the first parameter, it will always start with 1, and may end at any point. Instead of building the SQL like that, use a loop to gather your data first…

    $max = 12;
    $values = array();
    foreach(range(1, $max) as $index)
        $values[] = "'" . $objWorksheet->getCellByColumnAndRow($index, $row)->getFormattedValue() . "'";
    

    Now you can just glue together the array.

    $sql = 'INSERT INTO Project_Time_Sheet VALUES(' . join(', ', $values) .')';
    

    Hmm. getFormattedValue is SQL-safe, right? What if it wasn’t?

    There’s still some improvement to be made. PostgreSQL supports prepared statements. It might be wise to use them here.

    Again, let’s blindly assume that things are moderately dynamic. PHP’s PG extension uses a non-standard placeholder, which makes our life more difficult. Let’s make a small change to the data gathering:

    $max = 12;
    $values = array();
    foreach(range(1, $max) as $index)
        $values[ '$' . $index ] = "'" . $objWorksheet->getCellByColumnAndRow($index, $row)->getFormattedValue() . "'";
    

    The array is now keyed, using PG’s prepared statement placeholders.

    We’ll now reassemble the SQL using the keys instead of the values:

    $sql = 'INSERT INTO Project_Time_Sheet VALUES(' . join(', ', array_keys($values)) .')';
    

    $sql now looks something like

    INSERT INTO Project_Time_Sheet VALUES($1, $2, $3 ...);
    

    Let’s prepare and execute!

    $sth = pg_prepare($dbh, '', $sql);
    $res = pg_execute($dbh, '', array_values($values));
    

    Prepared statements get us two things:

    1. Added protection against SQL injection, and
    2. Potential performance benefits when used in a loop. Prepare once, execute multiple times.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is taken from an exam. I don't know how to do that.
this is probably the most noob question i have ever asked, i need to
(This is a lot like my last question, but I just realized I was
This is a long shot, but does anyone know of an algorithm for estimating
This is a long shot but I'm hoping there's a way to stop IPhones
This is probably a really lame question but one I need to ask. Why
This is a normalization thing, but I want I have to hold information about
I just wrote perhaps the ugliest bit of MVC code in existence. It is:
This may be a wild strange dream. I dreampt that I could put a
This is a derivative from this question . In that one, the following line

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.